2

通常调用一个类里面的方法需要如何操作:

$class = new class();
$class->fun()

依赖注入模式用来减少程序间的耦合

依赖注入共有三种模式:

setter 方法注入

着重说下setter方法注入并结合ArrayAccess


/**
 * Class Di
 * @property People
 */
class Di implements ArrayAccess
{
    /**
     * 单例
     * @var null
     */
    protected static $instance = null;

    /**
     * 注册的服务
     * @var array
     */
    protected $data = array();

    public function __construct()
    {
        echo '__construct'. "\n";
    }

    public function onConstruct(){
        echo 'onConstruct'. "\n";
    }

    public static function one(){
        if (self::$instance == null) {
            self::$instance = new Di();
            self::$instance->onConstruct();
        }
        return self::$instance;
    }

    public function get($name, $default = NULL) {
        if (!empty($default)) {
            return $default;
        }

        return $this->data[$name];
    }

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

    public function __get($name)
    {
        return $this->get($name);
    }

    public function __set($name, $value)
    {
        $this->set($name, $value);
    }


    /** ArrayAccess数组访问接口 **/

    public function offsetSet($offset, $value) {
        $this->set($offset, $value);
    }

    public function offsetGet($offset) {
        return $this->get($offset, NULL);
    }

    public function offsetUnset($offset) {
        unset($this->data[$offset]);
    }

    public function offsetExists($offset) {
        return isset($this->data[$offset]);
    }

}

class People {

    protected $name = '测试';
    public function getName(){
        return $this->name;
    }

}

class Email {

    public function sendEmail($email){
        return '邮件发送成功!';
    }

}

$di = Di::one();
/** @var get set 方式访问 people */
$di->people = new People();
$people = $di->people;
echo $di->people->getName();

/** 通过数组的方式访问 **/
$di['Email'] = new Email();
echo $di['Email']->sendEmail('33@qq.com');
Phalapi也是通过该方式实现依赖注入
依赖注入相当于一个注册中心,通过魔术方法__set __get进行赋值和取之操作,$di->email
implements ArrayAccess 可以实现通过数组的方式进行操作$di['Email']

构造方法注入

通过__construct出入类

class a
{
    public function test() {
        echo 'test';
    }
}

class c
{
    protected $s;
    public function __construct($a)
    {
        $this->s = $a;
    }

    public function test(){
        $this->s->test();
    }
}

$a = new a();
$c = new c($a);
$c->test();

接口注入

interface sql{

    public function connect();
    public function query();
}


class mysql implements sql {

    public function connect()
    {
        echo '连接mysql成功'. "\n";
    }

    public function query()
    {
        // TODO: Implement query() method.
    }
}

class sqlServe implements sql {

    public function connect()
    {
        echo '连接sqlServe成功'. "\n";
    }

    public function query()
    {
        // TODO: Implement query() method.
    }
}

class Demo{

    public $sql;

    public function __construct(sql $sql)
    {
        $this->sql = $sql;

    }
}

$mysql = new mysql();
$sqlServe = new sqlServe();

(new Demo($mysql))->sql->connect();

小小的太阳
123 声望7 粉丝

reloading...