PHP反射机制用法实例
(编辑:jimmy 日期: 2024/11/20 浏览:3 次 )
本文实例讲述了PHP反射机制的用法,分享给大家供大家参考之用。具体方法如下:
演示示例代码如下所示:
<"In Class One"; } } class ClassOneDelegator { private $targets; function __construct() { $this->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(); "htmlcode"><"In Class One"; } } class ClassOneDelegator { private $targets; function addObject($obj) { $this->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(); ?>希望本文所述对大家的PHP程序设计有所帮助。
下一篇:PHP对象相互引用的内存溢出实例分析