This article mainly introduces the use of PHP reflection mechanism, is a more important concept in PHP programming, the need for friends can refer to the following
In this paper, the use of the reflection mechanism of PHP is described and shared for everyone's reference. Here's how:
The demo sample code looks like this:
<?phpclass 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
It can be seen that the proxy class Classonedelegator instead of the Classone class to implement his method.
Similarly, the following code is also capable of running:
<?phpclass classone {function callclassone () {print "in class One"; }}class Classonedelegator {private $targets; function AddObject ($obj) {$this->target[] = $obj; The 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 ();