使用reflection执行授权模式
时间:2010-07-17 来源:qgymje
print_r(ReflectionClass::export('ClassOne'));
Class [ <user> class ClassOne ] {
@@ E:\xampp\htdocs\test\reflection.php 2-8
- Constants [0] {
}
- Static properties [0] {
}
- Static methods [0] {
}
- Properties [0] {
}
- Methods [1] {
Method [ <user> public method callClassOne ] {
@@ E:\xampp\htdocs\test\reflection.php 4 - 7
}
}
}
class ClassOne { function callClassOne() { print "In ClassOne\n"; } } class ClassTwo { function callClassTwo() { print "In ClassTwo\n"; } } class ClassOneDelegator { private $target; function __construct() { $this->target[] = new ClassOne(); } 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 ClassTwo()); $obj->callClassOne(); $obj->callClassTwo();
相关阅读 更多 +