PHP dependency injection

Keywords: PHP SQL MySQL

How to call a method in a class:

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

Dependency injection pattern is used to reduce the coupling between programs

There are three modes of dependency injection:

setter method injection

Focus on setter method injection combined with ArrayAccess


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

    /**
     * Registered services
     * @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 Array provider**/

    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 = 'test';
    public function getName(){
        return $this->name;
    }

}

class Email {

    public function sendEmail($email){
        return 'Email sent successfully!';
    }

}

$di = Di::one();
/** @var get set How to access people */
$di->people = new People();
$people = $di->people;
echo $di->people->getName();

/** Access by array**/
$di['Email'] = new Email();
echo $di['Email']->sendEmail('33@qq.com');

Phalapi also implements dependency injection in this way
Dependency injection is equivalent to a registry, which can be assigned and retrieved by magic method \
implements ArrayAccess can realize the operation by array $di['Email ']

Construction method injection

Enter and exit the class through ﹣ 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 injection

interface sql{

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


class mysql implements sql {

    public function connect()
    {
        echo 'Connect mysql Success'. "\n";
    }

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

class sqlServe implements sql {

    public function connect()
    {
        echo 'Connect sqlServe Success'. "\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();

Posted by Rebel7284 on Tue, 03 Dec 2019 23:09:05 -0800