PHP reflection mechanism usage examples

高洛峰
Release: 2023-03-04 07:42:02
Original
786 people have browsed it

The examples in this article describe the use of PHP reflection mechanism and are shared with you for your reference. The specific method is as follows:

The demonstration sample 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(); ?>
Copy after login

Output result:

In Class One

It can be seen that his method is implemented through the proxy class ClassOneDelegator instead of the ClassOne class.

Similarly, the following code can also 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(); ?>
Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

For more articles related to PHP reflection mechanism usage examples, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!