The code for demonstration is as follows:
Copy code The code is as follows:
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();
?>
Output result:
In Class One
is visible, replaced by the proxy class ClassOneDelegator ClassOne class to implement his methods.
Similarly, the following code can also be run:
Copy the code The code is as follows:
class ClassOne {
function callClassOne() {
print "In Class One";
}
}
class ClassOneDelegator {
private $targets;
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 ClassOne());
$obj->callClassOne();
?>
http://www.bkjia.com/PHPjc/319636.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319636.htmlTechArticleThe demonstration code is as follows: Copy the code as follows: ?php class ClassOne { function callClassOne() { print " In Class One"; } } class ClassOneDelegator { private $targets; funct...