ioc 代码

<?php 
class c
{
    public function say()
    {
        echo 'hello';
    }
}

/**
 * Class a
 */
class a
{
    private $c;
    public function setC(C $c)
    {
        $this->c = $c; // 实例化创建C类
    }

    public function sayC()
    {
        echo $this->c->say(); // 调用C类中的方法
    }
}

$c = new C();
$a = new a();
$a->setC($c);
$a->sayC();
?> 
 

 

 

<?php


error_reporting(0);


class Container {


    protected $bindings = [];


    public function __construct() {
        echo '<pre>';
    }


    public function bind($abstract, $concrete = null, $shared = false) {


        echo $abstract.$concrete.'<br>';
        if (!$concrete instanceof Closure) {


            $concrete = $this->getClosure($abstract, $concrete);
        }
        $this->bindings[$abstract] = compact('concrete', 'shared');
    }


    protected function getClosure($abstract, $concrete) {
        return function ($c) use ($abstract, $concrete) {
         //   print_r($c);
            $method = ($abstract === $concrete) ? 'build' : 'make';
              echo 'getclosure'. $abstract.$concrete.$method.'<br>';
            return $c->$method($concrete);
        };
    }


    public function make($abstract) {
        $concrete = $this->getConcrete($abstract);
        if ($this->isBuildable($concrete, $abstract)) {
            $object = $this->build($concrete);
        } else {
            $object = $this->make($concrete);
        }
        return $object;
    }


    protected function isBuildable($concrete, $abstract) {
        return $concrete === $abstract || $concrete instanceof Closure;
    }


    protected function getConcrete($abstract) {
        if (!isset($this->bindings[$abstract])) {
            return $abstract;
        }
        return $this->bindings[$abstract]['concrete'];
    }


    public function build($concrete) {
        if ($concrete instanceof Closure) {
            return $concrete($this);
        }


        $reflector = new ReflectionClass($concrete);
        if (!$reflector->isInstantiable()) {
            echo $message = "Target [$concrete] is not instantiable";
        }


        $constructor = $reflector->getConstructor();
        if (is_null($constructor)) {
            return new $concrete;
        }


        $dependencies = $constructor->getParameters();
        $instances = $this->getDependencies($dependencies);
        return $reflector->newInstanceArgs($instances);
    }


    protected function getDependencies($parameters) {


        $dependencies = [];
        foreach ($parameters as $parameter) {
            $dependency = $parameter->getClass();
            if (is_null($dependency)) {
                $dependencies[] = null;
            } else {
                $dependencies[] = $this->resolveClass($parameter);
            }
        }


        return (array) $dependencies;
    }


    protected function resolveClass(ReflectionParameter $parameter) {
        return $this->make($parameter->getClass()->name);
    }


}


class Traveller {


    protected $trafficTool;


    public function __construct(Visit $trafficTool) {
        $this->trafficTool = $trafficTool;
    }


    public function visitTibet() {
        $this->trafficTool->go();
    }


}


interface Visit {


    public function go();
}


class Train implements Visit {


    public function go() {
        echo 'drive car to tibet!!!'
        ;
    }


}


$app = new Container;
$app->bind("Visit", 'train');
$app->bind('traveller', 'Traveller');




$tra = $app->make('traveller');
$tra->visitTibet();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值