Use PHP reflection to implement the delegate mode. Delegated mode is a basic technique in software design patterns. In the delegate mode, two objects are involved in processing the same request. the object receiving the request delegates the request to another object. The delegate mode is a basic technique in the software design mode. In the delegate mode, two objects are involved in processing the same request. the object receiving the request delegates the request to another object for processing. Delegation mode is a basic technique. many other modes, such as status mode, policy mode, and visitor mode, use delegation mode in special cases.
Introduction to Dynamic Delegation: the concept of dynamic delegation comes from the Jakarta bytecode Engineering Library (Byte-Code Engineering Library, BCEL ). It can analyze existing classes, and for interfaces, abstract classes, and even specific classes during runtime, it can generate a bytes-encoded delegate class.
The delegated interface/class must meet the following conditions: a dynamic delegate can only delegate one class, but can proxy multiple interfaces. This restriction comes from the Java single inheritance mode. A Java class can have at most one parent class. Since the generated delegate class regards the delegated class as its parent class, it is unreasonable to specify multiple delegate classes. If the delegate class is not specified, the default parent class is Object.
Below is the PHP Reflection mechanism to implement dynamic proxy code:
Target [] = new Fruit ();} 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 FruitDelegator (); $ obj-> callFruit (); // running result // Generate an Apple?>
It can be seen that the proxy class FruitDelegator is used to replace the Fruit class to implement its method.
Similarly, the following code can be run:
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 ColorDelegator();$obj->addObject(new Color());$obj->callColor();?>
Bytes. In the delegate mode, two objects are involved in processing the same request. the object receiving the request delegates the request to another object...