php设计模式-简单依赖注入

<?php
class A 
{
    private $_b;

    public function __construct($b)
    {
        $this->_b = $b;
    }

    public function sayHello()
    {
        echo ‘I am A‘;
        $this->_b->sayHello();
    }
}

class B
{
    private $_c;

    public function __construct($c)
    {
        $this->_c = $c;
    }

    public function sayHello()
    {
        echo ‘I am B‘;
        $this->_c->sayHello();
    }
}

class C
{
    public function sayHello()
    {
        echo ‘I am C‘;
    }
}

$a = new A(new B(new C()));
$a->sayHello();

  运行结果:

I am C
I am B
I am A

  

php设计模式-简单依赖注入

上一篇:在HTML5上开发音视频应用的五种思路


下一篇:js_算术运算