Use PHP reflection to implement delegation mode _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.