代码之家  ›  专栏  ›  技术社区  ›  Stark

如何将PDO与依赖注入结合使用?

  •  0
  • Stark  · 技术社区  · 9 年前

    我很难理解如何使用依赖注入。我已经在这里阅读了很多问题/答案,但我无法用我正在使用的代码来描述它。

    abstract class Model {
    
        protected static function getDB() {
            static $db = null;
    
            if ($db === null) {
                $db = new PDO('mysql:host=host;dbname=dbname;charset=utf8', 'dbuser', 'password');
            }
    
            return $db;
        }
    }
    

    模型。php只包含这个函数,我不想设置和调用它

    使用者php

    class User extends Model {
    
        /*
        * Selects all of the user information
        */
        public function getUser($id){
    
            $db = static::getDB();
    
            $sth = $db->prepare('SELECT * FROM user WHERE id = :id');
            $sth->bindValue(':id', $id, PDO::PARAM_INT);
            $sth->execute();
    
            return $sth->fetch();
        }
    
        /*
        * Selects all of the user posts
        */
        public function getUserPosts($id){
            $db = static::getDB();
    
            $sth = $db->prepare('SELECT * FROM user_posts WHERE user_id = :id');
            $sth->bindValue(':id', $id, PDO::PARAM_INT);
            $sth->execute();
    
            return $sth->fetch();
        }
    }
    

    在用户中。php我正在扩展模型类,但我已经设置了 $db = static::getDB();

    我知道依赖注入几乎只是将方法/变量传递给对象,但我甚至不确定我是否做对了。

    更新了更多想法:

    我认为最好创建一个私有变量,在构造函数中我们只需调用 getDB()

    class User extends Model {
    
        protected $db;
    
        public function __construct(){
            $this->db = getDB();
        }
    
        /*
        * example usage
        */
        public function getUser($id){
            $sth = $this->db->prepare('SELECT * FROM user WHERE id = :id');
            $sth->bindValue(':id', $id, PDO::PARAM_INT);
            $sth->execute();
    
            return $sth->fetch();
        }
    }
    

    但是,如果我没有在函数构造函数中直接调用该类,它还会被视为依赖注入吗?

    page 这让我觉得更有意义,这就是我想到的。

    模型php

    abstract class Model {
        protected $db = null;
    
        public function __construct(){
            if($this->db === null){
                try {
                    $this->db = new PDO('mysql:host=' . Config::DB_HOST . ';dbname=' . Config::DB_NAME . '; charset=utf8', Config::DB_USER, Config::DB_PASSWORD);
                    $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                } catch (PDOException $e) {
                    echo 'Connection failed: ' . $e->getMessage();
                }
            }
            return $this->db;
        }
    }
    

    class User extends Model {
    
        protected $db;
    
        public function __construct(Model $db){
            $this->db = $db;
        }
    
        /*
        * example usage
        */
        public function getUser($id){
            $sth = $this->db->prepare('SELECT * FROM user WHERE id = :id');
            $sth->bindValue(':id', $id, PDO::PARAM_INT);
            $sth->execute();
    
            return $sth->fetch();
        }
    }
    

    1 回复  |  直到 9 年前
        1
  •  3
  •   JFloresI    9 年前

    我认为您没有使用依赖项注入,因为您实际上没有向模型中提供任何依赖项,而是在构造函数上生成它们。

    为了提供依赖关系,应该将其作为参数传递给构造函数:

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

    推荐文章