class food{ public $name = ' Apple ';} class person{ const sex = ' Boy '; public $name; private $age; protected $hobbies = []; public function eat (food $foodObj, string $waterName ):string { return "eatting {$ foodobj->name},drinking {$waterName} "; } public Function hello () { return ' Hello world '; }} $instance = (new reflectionclass (' person ')) Newinstance (); $reflectionMethod = new reflectionmethod (' person ', ' eat ');//Verify that the member method is public, Accessible if (! $reflectionMethod->ispublic ()) { echo ' cannot access the Eat method of the person class ' . php_eol; return;} An array of Reflectionparametr objects $parameters = $reflectionMethod->getparameters ();if (Empty ($ Parameters)) { echo $reflectionMethod->invoke ($instance);} else { $args = []; foreach ($ parameters as $parameterObj) { if ($ reflectionclass = $parameterObj->getclass ()) { $className = $reflectionclass->getname (); $args [] = (New reflectionclass ($className))- >newinstance (); } else { $args [] =&nbsP; ' Hello '; } } echo $reflectionMethod->invoke ($instance, .... $args);}
Implementing Reflectionclass Objects
Implementing Reflectionmethod Objects
Implementing Reflectionparameter Objects
Implementing dependency injection of objects through reflection
PHP uses reflection to implement member method invocation of an object