在PHP项目中设置依赖项注入

时间:2018-07-10 09:01:00

标签: php dependency-injection

我试图围绕PHP中的依赖项注入概念进行研究。我了解基本原理,其中依赖项注入用于“预加载”所有必需的参数,函数,对象..运行某个操作所需的一切。

但是,当我尝试在测试MVC上实现它时,我会感觉自己除了在转移数据之外什么都不做。

我写了一个小例子来说明自己的工作方式。因此,如果有人愿意告诉我我做错了什么。

PS。我知道框架通常使用的依赖项注入容器,但是如果我无法掌握它的基础知识,那么使用类似的东西就没有意义了。

示例:

<?php

class database_test
{
    public function __construct($DB_con_info)
    {
        try {
            $dsn = "mysql:host=" . $DB_con_info['host'] .
                ";dbname=" . $DB_con_info['dbname'] .
                ";charset=" . $DB_con_info['charset'];
            echo 'database connection set -> ';

            $this->dbc = new PDO ($dsn, $DB_con_info['username'], $DB_con_info['password']);
            $this->dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
            echo "conn failed msg :  " . $e->fetMessage();
        }
    }
}

class model_interface extends database_test
{
    protected $dbc;

    protected $DB_con_info = array(
        'driver' => 'mysql',
        'host' => 'localhost',
        'dbname' => 'mvc_db',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8mb4'
    );

    //depende_injection
    public function __construct()
    {
        parent::__construct($this->DB_con_info);
        echo 'model_interface set -> ';
    }

    public function __destruct()
    {
        $this->dbc = null;
    }

    public function add($username, $last_name)
    {
        $sth = $this->dbc->prepare('INSERT INTO test (name, last_name ) VALUES (?,?)');
        $sth->execute([$username, $last_name]);
    }
}

class controller_test
{
    protected function __construct($name)
    {
        $this->view = new view();
        $this->model = $this->loadModel($name);
    }

    protected function loadModel($model)
    {
        //require the php file with the model class (test_model) in it
        require_once 'models/' . $model . '_model.php';
        //$model= test_model;
        $model = $model . '_model';

        //return new object of class test_model
        return new $model();
    }
}

class test_model extends model_interface
{

    private $model_interface_functions;

    //depende_injection
    public function __construct()
    {
        $this->model_interface_functions = new model_interface;
        echo 'model_interface_functions set ->';
    }

    public function __destruct()
    {
        $this->model_interface_functions = null;
    }

    public function test2()
    {
        $name = 'name';
        $last_name = 'last_name';
        $this->model_interface_functions->add($name, $last_name);
    }
}

class Test extends controller_test
{
    protected $model;
    protected $view;

    //depende_injection
    public function __construct()
    {
        echo 'setting depende_injection<br>';
        parent::__construct('test');
        echo 'all dependencies set ->';
    }

    public function test()
    {
        echo 'test controller <br>';
        $this->model->test2();
    }

    public function __destruct()
    {
        $this->model = null;
        $this->view = null;
    }

    public function index()
    {
        $this->view->render('test', 'index');
    }
}

0 个答案:

没有答案