php-反射机制

2023-03-03 19:23:36 浏览数 (1)

浏览量 3

代码语言:javascript复制
<?php
class ClassOne {
    function callClassOne() {
        print "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();
?>

输出结果: In Class One

阅读原文

0 人点赞