The demo is shown in the following code
<?php
classclassone{
Functioncallclassone () {
print "Inclassone";
}
}
classclassonedelegator{
Private$targets;
Function__construct () {
$this->target[]=newclassone ();
}
Function__call ($name, $args) {
foreach ($this->targetas$obj) {
$r =newreflectionclass ($obj);
if ($method = $r->getmethod ($name)) {
if ($method->ispublic () &&! $method->isabstract ()) {
Return$method->invoke ($obj, $args);
}
}
}
}
}
$obj =newclassonedelegator ();
$obj->callclassone ();
?>
Output results:
In Class One
Visible, through proxy class classonedelegator to replace the Classone class to implement his method.
Similarly, the following code is also capable of running:
<?php
classclassone{
Functioncallclassone () {
print "Inclassone";
}
}
classclassonedelegator{
Private$targets;
Functionaddobject ($obj) {
$this->target[]= $obj;
}
Function__call ($name, $args) {
foreach ($this->targetas$obj) {
$r =newreflectionclass ($obj);
if ($method = $r->getmethod ($name)) {
if ($method->ispublic () &&! $method->isabstract ()) {
Return$method->invoke ($obj, $args);
}
}
}
}
}
$obj =newclassonedelegator ();
$obj->addobject (Newclassone ());
$obj->callclassone ();
?>