编程笔记

php-反射机制

11/27/2021 8:52:05 AM
0
<?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

阅读原文

友情链接

Copyright @2021-2024 关于