class MyClass { public $property1; private $property2; protected $property3; } $reflect = new ReflectionClass('MyClass'); $props = $reflect->getProperties(); foreach ($props as $prop) { echo $prop->getName() . "\n"; }
property1 property2 property3
class ParentClass { public $parentProperty; } class ChildClass extends ParentClass { public $childProperty; } $reflect = new ReflectionClass('ChildClass'); $props = $reflect->getProperties(); foreach ($props as $prop) { echo $prop->getName() . "\n"; }
childProperty parentPropertyThis code defines two classes, "ParentClass" and "ChildClass", where "ChildClass" extends "ParentClass" and defines a new property. The code then creates a new ReflectionClass object for "ChildClass" and uses the getProperties method to retrieve all the properties, including those inherited from the parent class. Finally, the code loops through each property and prints its name using the getName method of the ReflectionProperty object. Package/library: This code example belongs to the PHP core library.