PHP simple IoC control reversal implementation _ PHP Tutorial

Source: Internet
Author: User
PHP's simple implementation of IoC control inversion. We have discussed the design principles of IoC control reversal. now we use PHP to implement IoC design principles and deepen our understanding of this concept. First, compile the POJO and Fruit interfaces of Fruit. We have discussed the design principles of IoC control reversal. now we use PHP to implement IoC design principles and deepen our understanding of this concept.

First, compile the POJO and Fruit interfaces of Fruit. Inherit from the Fruit interface to generate two types of Apple and Banana, both of which implement the showColor () method of the interface.

Fruit. php
 color;    }    function setColor($value){        $this->color=$value;    }        function showColor(){        echo "Apple Color:" . $this->color;    }}class Banana implements Fruit {    private  $color;    function getColor(){        return $this->color;    }    function setColor($value){        $this->color=$value;    }    function showColor(){        echo "Banana Color:" . $this->color;    }}?>
ClassFactory. php

The class factory implements some simple functions to parse the specified configuration file, generate a class based on the configuration file, and set the specified property value:

 "," ", $ Content); // Convert it to an array $ this-> arr_conf = json_decode ($ content, true);} function getBean ($ class_id) {// search for classes that match $ class_id while (true) {// test $ conf = each ($ this-> arr_conf) one by one; // cannot be found, return null if ($ conf = false) return null; if (strcmp ($ class_id, $ conf ["value"] ["id"]) === 0) {// if matched, mount the class file include_once ($ conf ["value"] ["class_file"]); // Generate an instance of the class // print_r ($ conf ["value"] ["class_name"]); $ obj = new $ conf ["value"] ["class_name "] (); // Find the property set $ arr_prop = $ conf [" value "] [" properties "]; while ($ prop = each ($ arr_prop )) {// setXXX indicates the setting method for generating attributes. The first letter is uppercase $ set_method = "set ". ucwords ($ prop ["value"] ["name"]); // call the setting method, and set the default value $ obj-> $ set_method ($ prop ["value"] ["value"]);} // find the first one, return instance return $ obj ;}}}?>
Config. json
// Note the use of double backslice [{// class ID for the factory to find "id": "Apple", // class name "class_name": "Apple ", // class file "class_file": "Fruit. php ", // property settings" properties ": [{" name ":" color "," value ":" Red "}] },{" id ": "Banana", "class_name": "Banana", "class_file": "Fruit. php "," properties ": [{" name ":" color "," value ":" Yellow "}]
Test. php

Specify the preceding configuration file for the class factory. We can see that different class instances are returned for different IDs in the configuration file. Call the methods of each instance and output Red and Yellow respectively. It can be seen that we only need to change the content of the configuration file to return different class instances without modifying the program source code. This achieves a simple IOC or DI.

 getBean("Apple");echo $fruit->showColor() .'
';$fruit = $factory->getBean("Banana");echo $fruit->showColor() .'
';?>

PHP is a dynamic language. using some language features, it is very easy to implement, and there is no such concept as reflection.

Program running result

Apple Color:RedBanana Color:Yellow

Bytes. First, compile the POJO and Fruit interfaces of Fruit. Following...

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.