PHP simple IoC control inversion implementation_PHP tutorial

WBOY
Release: 2016-07-13 10:33:53
Original
1170 people have browsed it

We discussed the design principles of IoC Inversion of Control. Now we use PHP to implement the IoC design principles to deepen our understanding of this concept.

First write Fruit’s POJO and Fruit interface. Inheriting the Fruit interface generates two classes, Apple and Banana, both of which implement the showColor() method of the interface.

Fruit.php

<?php
	
/**
 * @author Gonn, http://www.bkjia.com/
 */
interface Fruit {
    public function showColor();
}
class Apple implements Fruit {
	private $color;
    function getColor(){
        return $this->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;
    }
}
?>
Copy after login

ClassFactory.php

The class factory implements some simple functions, which can parse the specified configuration file, generate classes based on the configuration file, and set the specified attribute values:

<?php
	
/**
 * @author Gonn, http://www.bkjia.com/
 */
class ClassFactory {
    private $arr_conf;
    function ClassFactory($config_file){
        //读取配置文件内容
        $handle = fopen($config_file, r);
        $content = fread($handle, filesize($config_file));
        fclose($handle);
        //去除注释
        $content=preg_replace("<\/\/.*?\s>","",$content);
        //转成数组
        $this->arr_conf=json_decode($content,true);
    }
     function getBean($class_id){
        //查找匹配 $class_id 的类
        while(true){
            //挨个测试
            $conf=each($this->arr_conf);
            //找不到,返回 null
            if($conf==false) return null;
            if(strcmp($class_id,$conf["value"]["id"])===0){
                //如果匹配则装入类文件
                include_once($conf["value"]["class_file"]);
                //生成类的实例
                //print_r($conf["value"]["class_name"]);
                $obj=new $conf["value"]["class_name"]();
                //查找被设置的属性
                $arr_prop=$conf["value"]["properties"];
                while($prop=each($arr_prop)){
                    //生成属性的设置方法 setXXX ,第一个字母大写
                    $set_method="set". ucwords($prop["value"]["name"]);
                    //调用设置方法,并设置预设值
                    $obj->$set_method($prop["value"]["value"]);
                }
                //找到第一个,返回实例
                return $obj;
            }
        }
    }
}
?>
Copy after login

config.json

//备注使用双反斜杆
[
    {
        //类ID,方便工厂查找
        "id": "Apple",
        //类名
        "class_name":"Apple",
        //类文件
        "class_file":"Fruit.php",
        //属性设置
        "properties":[
            {
                "name":"color",
                "value":"Red"
            }
        ]
    },
    {
        "id": "Banana",
        "class_name":"Banana",
        "class_file":"Fruit.php",
        "properties":[
            {
                "name":"color",
                "value":"Yellow"
            }
        ]
    }
]
Copy after login

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 method of each instance is called, and Red and Yellow are output 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 enables simple IOC or DI.

<?php
include_once("ClassFactory.php");
$factory = new ClassFactory("config.json");
$fruit = $factory->getBean("Apple");
echo $fruit->showColor() .'<br />';
$fruit = $factory->getBean("Banana");
echo $fruit->showColor() .'<br />';
?>
Copy after login

PHP is a dynamic language. It takes advantage of some language features and is very simple to implement without using concepts such as reflection.

Program execution results

Apple Color:Red
Banana Color:Yellow
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752399.htmlTechArticleWe discussed the design principles of IoC inversion of control. Now we use PHP to implement the IoC design principles and deepen our understanding of them. understanding of this concept. First write Fruit's POJO and Fruit interface. Following...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!