We discussed the design principles for the IOC control reversal, and now we use PHP to implement the IOC design principles and deepen our understanding of the concept.
First write the Fruit POJO and Fruit interfaces. Inheriting the Fruit interface generates two classes, Apple and Banana, which all implement the interface's Showcolor () method.
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 that parse the specified configuration file and generate classes based on the configuration file, setting the specified property values:
"," ", $content); $this->arr_conf=json_decode ($content, True) for an array of tuples; } function Getbean ($class _id) {//Find the class that matches $class _id while (true) {//Test $conf =each ( $this->arr_conf); Not found, returns null if ($conf ==false) return null; if (strcmp ($class _id, $conf ["Value"] ["id"]) ===0) {//If a match is loaded into the class file include_once ($conf ["Value"] ["cl Ass_file "]); Generates an instance of the class//print_r ($conf ["Value"] ["class_name"]); $obj =new $conf ["Value"] ["class_name"] (); Find the property that is set $arr _prop= $conf ["Value"] ["Properties"]; while ($prop =each ($arr _prop)) {//Build property is set by Setxxx, the first letter is capitalized $set _method= "set". Ucwords ($prop ["Value"] ["name"]); Call the Set method and set the preset value $obj $set _method ($prop ["Value"] ["value"]); }//To find the first one, return the instance return $obj; }}}}?>
Config.json
Note Use double backslash [ { //class ID, convenient for factory to find "id": "Apple", //class name "Class_name": "Apple", //class file "Class_ File ":" fruit.php ", //Property Set " Properties ": [ { " name ":" Color ", " value ":" Red " } ] }, { "id": "Banana", "class_name": "Banana", "Class_file": "fruit.php", " Properties ": [ { " name ":" Color ", " value ":" Yellow " } ] }]
test.php
Specify the above configuration file for the class factory. We can see that different class instances are returned for different IDs in the configuration file. The methods for each instance are called, and red and yellow are exported separately. As you can see, we only need to change the contents of the configuration file to return different instances of the class without modifying the program source code. This enables 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 simple to implement, but also useless to the concept of reflection.
Program Run Results
Apple Color:redbanana Color:yellow
http://www.bkjia.com/PHPjc/752399.html www.bkjia.com true http://www.bkjia.com/PHPjc/752399.html techarticle we discussed the design principles for the IOC control reversal, and now we use PHP to implement the IOC design principles and deepen our understanding of the concept. First write the Fruit POJO and Fruit interfaces. The following ...