您的位置:首页 > 编程语言 > PHP开发

php 反射机制实现代理模式

2013-06-08 10:29 537 查看
<?php
class test{
function callprint() {
print_r("onesdf");
}
function test2($args='') {
print_r($args);
}
}
class testDelegator {
private $targets;
function __construct($obj) {
$this->targets[]=$obj;
}
function __call($name,$args) {
foreach($this->targets as $obj)
{
$r=new ReflectionClass($obj);
if($method=$r->getMethod($name))
{
if($method->isPublic()&&!$method->isAbstract())
{
return $method->invoke($obj,$args);
}
}
}
}
}

$d=new testDelegator(new test());
$d->callprint();
$d->test2('the name of the method is test2','array2');
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: