- The so-called reflex is to define good after a class, class structure in order to gain information by reading the class name ReflectionClass process
class mycoach { protected $ name; protected $ Age; public function the __construct ($ name, $ Age) { $ the this -> name = $ name; $ the this -> Age = $ Age; } public function EAT () { echo " ah , increased physical eat something " .PHP_EOL; } }
- Get a reflecting object
$ coach = new mycoach ( 'Chenpei Chang', 22 is);
$ A = the ReflectionClass new new ( 'mycoach');
IF ($ A-> isInstantiable ())
{
echo "scientific training and diet" .PHP_EOL;
}
$myattributes = $a->getConstructor();
var_dump($myattributes);
Output:
Scientific training and diet Object (ReflectionMethod) # . 3 ( 2 ) { [ " name " ] => String ( . 11 ) " the __construct " [ " class " ] => String ( . 7 ) " mycoach " }
- Method One getProperties () Note! To achieve print two attributes, be sure to declare two property protection in the class structure in (for example: protected $ name)
$property = $a->getProperties(); print('=================property================='.PHP_EOL); var_dump($property);
Output:
=================property================= array(2) { [0]=> object(ReflectionProperty)#4 (2) { ["name"]=> string(4) "name" ["class"]=> string(7) "mycoach" } [1]=> object(ReflectionProperty)#5 (2) { ["name"]=> string(3) "age" ["class"]=> string(7) "mycoach" } }
- Method II getMethods () getname ()
print('=================methods================='.PHP_EOL); var_dump($a->getMethods()); print('=================methods by row================='.PHP_EOL); foreach($a->getMethods() as $method) { echo $method->getName().PHP_EOL; }
Output:
=================methods================= array(2) { [0]=> object(ReflectionMethod)#6 (2) { ["name"]=> string(11) "__construct" ["class"]=> string(7) "mycoach" } [1]=> object(ReflectionMethod)#7 (2) { ["name"]=> string(3) "eat" ["class"]=> string(7) "mycoach" } } =================methods by row================= __construct eat
- hasMethod() getMethod()
print('=================execute================='.PHP_EOL); if($a->hasMethod('eat')) { $method= $a->getMethod('eat'); print_r($method); }
Output:
=================execute================= ReflectionMethod Object ( [name] => eat [class] => mycoach ) Process finished with exit code 0
- Copy and paste --- quick test
<? PHP class mycoach { protected $ name; protected $ Age; public function the __construct ($ name, $ Age) { $ the this -> name = $ name; $ the this -> Age = $ Age; } public function EAT () { echo " ah, eat something increasing physical fitness " .PHP_EOL; } } $ Coach = new new mycoach ( ' Chenpei Chang ' , 22 is ); $ a = new new ReflectionClass('mycoach'); if($a->isInstantiable()) { echo "科学的饮食和训练".PHP_EOL; } $myattributes = $a->getConstructor(); var_dump($myattributes); $property = $a->getProperties(); print('=================property================='.PHP_EOL); var_dump($property); print('=================methods================='.PHP_EOL); var_dump($a->getMethods()); print('=================methods by row================='.PHP_EOL); foreach($a->getMethods() as $method) { echo $method->getName().PHP_EOL; } print('=================execute================='.PHP_EOL); if($a->hasMethod('eat')) { $method= $a->getMethod('eat'); print_r($method); }