This article mainly introduces the PHP Reflection mechanism usage, which is an important concept in PHP programming. if you need it, you can refer to the example in this article to describe the PHP Reflection mechanism usage, share it with you for your reference. The specific method is as follows:
The demo code is as follows:
target[] = new ClassOne(); } function __call($name, $args) { foreach ($this->target as $obj) { $r = new ReflectionClass($obj); if ($method = $r->getMethod($name)) { if ($method->isPublic() && !$method->isAbstract()) { return $method->invoke($obj, $args); } } } }}$obj = new ClassOneDelegator();$obj->callClassOne();?>
Output result:
In Class One
It can be seen that the ClassOneDelegator class is used to replace the ClassOne class to implement its method.
Similarly, the following code can be run:
target[] = $obj; } function __call($name, $args) { foreach ($this->target as $obj) { $r = new ReflectionClass($obj); if ($method = $r->getMethod($name)) { if ($method->isPublic() && !$method->isAbstract()) { return $method->invoke($obj, $args); } } } }}$obj = new ClassOneDelegator();$obj->addObject(new ClassOne());$obj->callClassOne();?>
I hope this article will help you with PHP programming.
For more articles about PHP Reflection mechanism usage examples, refer to PHP Chinese network!