演示示例代码如下所示:
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();?>
输出结果:
in class one
可见,通过代理类classonedelegator来代替classone类来实现他的方法。
同样的,如下的代码也是能够运行的:
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程序设计有所帮助。
