$class = new ReflectionClass('Person');
<?php class Person { protected $name; protected $age; public function getName() { return $this->name; } public static function getAge() { return $this->age; } } //我们只需要把类名’Person’传给ReflectionClass即可得到Person的反射。 $class = new ReflectionClass('Person'); $instance = $class->newInstanceArgs(); $properties = $class->getProperties(); foreach( $properties as $pro ) { echo $pro->getName()."\n"; } echo '<br>','----------------------','<br>'; $method = $class->getMethods(); var_dump($method); echo '----------------------','<br>'; $getName = $method[0]; $getName->invoke($instance); //或者 $instance->getName();
name age ---------------------- array (size=2) 0 => & object(ReflectionMethod)[5] public 'name' => string 'getName' (length=7) public 'class' => string 'Person' (length=6) 1 => & object(ReflectionMethod)[6] public 'name' => string 'getAge' (length=6) public 'class' => string 'Person' (length=6) ----------------------