PHP 容器类(Container)的理解

12 篇文章 0 订阅
<?php

class Container{
    // 存放容器的数据
    public $instances = [];

    // 单例模式
    protected static $instance;

    private function __construct(){}

    public static function getInstance(){
        if(is_null(static::$instance)){
            static::$instance = new static();
        }
        return static::$instance;
    }

    public function set($key, $value){
        $this->instances[$key] = $value;
    }

    public function get($key){
        $new = $this->instances[$key];
        return $new;
    }


}

class Person{
    private $count = 0;

    /**
     * Person 依赖 于 Car、Book类等等
     * 所以 需要注入 Car或者Book类等对象才可以运行buy方法
     */
    public function buy($obj){
        $this->count += $obj->pay();
    }

    public function getCount(){
        echo "这个人总共消费了{$this->count}元";
        return $this->count;
    }
}

class Car {
    public function pay(){
        echo "我支付了100000元买了这辆车";
        return 100000;
    }
}

Container::getInstance()->set("person", new Person());
Container::getInstance()->set("car", new Car());

$obj = Container::getInstance()->get('person');
$obj->buy(Container::getInstance()->get('car'));

更改后

<?php

class Container{
    // 存放容器的数据
    public $instances = [];

    // 单例模式
    protected static $instance;

    private function __construct(){}

    public static function getInstance(){
        if(is_null(static::$instance)){
            static::$instance = new static();
        }
        return static::$instance;
    }

    public function set($key, $value){
        $this->instances[$key] = $value;
    }

    public function get($key){
        $new = $this->instances[$key];
        return $new;
    }


}

class Person{
    private $obj;
    private $count = 0;

    /**
     * 更改的代码
     */
    public function __construct($obj){
        $this->obj = $obj;
    }

    /**
     * Person 依赖 于 Car、Book类等等
     * 所以 需要注入 Car或者Book类等对象才可以运行buy方法
     */
    public function buy(){
        $this->count += $this->obj->pay();
    }

    public function getCount(){
        echo "这个人总共消费了{$this->count}元";
        return $this->count;
    }
}

class Car {
    public function pay(){
        echo "我支付了100000元买了这辆车";
        return 100000;
    }
}

Container::getInstance()->set("person", new Person(new Car()));

$obj = Container::getInstance()->get('person');
$obj->buy();

利用反射机制的容器

<?php

class Container{
    // 存放容器的数据
    public $instances = [];

    // 单例模式
    protected static $instance;

    private function __construct(){}

    public static function getInstance(){
        if(is_null(static::$instance)){
            static::$instance = new static();
        }
        return static::$instance;
    }

    public function set($key, $value){
        $this->instances[$key] = $value;
    }

    /**
     * 这个方法判断的依据 类的构造方法里面的参数
     * @param $key
     * @return object
     * @throws ReflectionException
     */
    public function get($key){
        if(!empty($this->instances[$key])){
            $key = $this->instances[$key];
        }

        // 反射类
        $reflect = new ReflectionClass($key);
        // 获取类的构造函数
        $c = $reflect->getConstructor();
        if(!$c){
            // 如果没有构造函数,返回创建对象
            return new $key;
        }
        // 获取函数里面的参数
        $params =$c->getParameters();
        if(empty($params)){
            // 如果构造函数里面没有参数,返回创建对象
            return new $key;
        }
        // 遍历构造函数里面的参数
        foreach ($params as $param) {
            $class = $param->getClass();
            if(!$class){

            }else{
                // $this->get("Car") 事实上相当于返回new Car这个对象
                $args[] = $this->get($class->name);
            }
        }
        // 返回创建对象
        return $reflect->newInstanceArgs($args);
    }


}

class Person{
    private $obj;
    private $count = 0;

    /**
     * 更改的代码
     * 注意这里Person类 依赖于Car 类
     * 因此容器里面必须存在Person和Car这两个类
     */
    public function __construct(Car $obj){
        $this->obj = $obj;
    }

    /**
     * Person 依赖 于 Car类
     * 所以 需要注入 Car对象才可以运行buy方法
     */
    public function buy(){
        $this->count += $this->obj->pay();
    }

    public function getCount(){
        echo "这个人总共消费了{$this->count}元";
        return $this->count;
    }
}

class Car {
    public function pay(){
        echo "我支付了100000元买了这辆车";
        return 100000;
    }
}

Container::getInstance()->set("person", "Person");
Container::getInstance()->set("car", "Car");

$obj = Container::getInstance()->get('person');
$obj->buy();

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值