class MyClass { public $prop1; protected $prop2; private $prop3; } $obj = new MyClass(); $reflectionObj = new ReflectionObject($obj); $propList = $reflectionObj->getProperties(); foreach ($propList as $prop) { echo $prop->getName() . "\n"; }
class MyClass { public function myMethod() {} protected function protectedMethod() {} private function privateMethod() {} } $obj = new MyClass(); $reflectionObj = new ReflectionObject($obj); $methodList = $reflectionObj->getMethods(); foreach ($methodList as $method) { echo $method->getName() . "\n"; }
class MyClass { const MY_CONSTANT = "Hello, world!"; } $obj = new MyClass(); $reflectionObj = new ReflectionObject($obj); $constList = $reflectionObj->getConstants(); foreach ($constList as $constName => $constValue) { echo $constName . " = " . $constValue . "\n"; }In this example, we create a new instance of the `MyClass` class, which contains a single constant called `MY_CONSTANT`. We then use `ReflectionObject` to retrieve a list of the class's constants, and print out their names and values. These examples use the `ReflectionObject` class, which is part of the Reflection extension in PHP. This extension is included in the PHP core, and does not require any additional packages or libraries.