dsn5510 2014-05-20 10:56
浏览 121
已采纳

如何正确使用PHP的依赖注入?

I'm very new at PHP. I have two classes: Database and RetrieveItem. Because RetrieveItem needs a connection, I've just been extending the Database class to use its constructor. Apparently this is wrong, because RetrieveItem is not a database?

Here is my current code:

class Database {
    public $host = '127.0.0.1';
    public $username = 'root';
    public $password = '';
    public $dbname = 'example';

 function __construct(){
    $this->connect = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password);
  }
}

class RetrieveItem extends Database {

    function retrieve_item(){
            $query = $this->connect->prepare("SELECT * FROM posts");
            $query->execute();
            $all_items = $query->fetchAll(PDO::FETCH_ASSOC);
            return $all_items;
    }
}

And on a separate page, to use this, I have:

include 'db.php';
$retrieve = new RetrieveItem();
print_r($retrieve->retrieve_item());

Rather than extend the class, how can I access the Database constructor in the cleanest possible way?

Any help or guidance would be much appreciated.

This amended code is still not working:

Argument 1 passed to RetrieveItem::__construct() must be an instance of Database, none given:

class Database {
    public $host = '127.0.0.1';
    public $username = 'root';
    public $password = '';
    public $dbname = 'example';

 function __construct(){
    $this->connect = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password);
  }
}

class RetrieveItem {
    private $_db;

    public function __construct(Database $database){
            $this->_db = $database;
    }

    public function retrieve_item(){
            $query = $this->connect->prepare("SELECT  * FROM posts");
            $query->execute();
            $all_items = $query->fetchAll(PDO::FETCH_ASSOC);
            return $all_items;
    }
}

In use:

include 'db.php';

$database = new Database();
$retrieve = new RetrieveItem($database);
print_r($retrieve->retrieve_item());
  • 写回答

3条回答 默认 最新

  • douhun8647 2014-05-20 11:15
    关注

    A dependency injection is so easy but it sounds complicated.

    $class1 = new firstclass();
    $class2 = new secondclass($class1); //This is a dependency injection.
    
    class firstclass{
        private $var1;
        private $var2;
    
        public function __construct(){
            $this->var1 = "hello";
            $this->var2 = "world";
        }
    
        public function getvar1(){
            return $this->var1;
        } //imagine a second one like this for var2;
    }
    
    class secondclass{
    
        private $fc; //will hold first class object or the dependency.
    
        public function __construct($firstclassobject){
            $this->fc = $firstclassobject;
            echo $this->fc->getvar1(); //call dependency methods like this.
            echo $this->fc->getvar2();
        } //echoes helloworld
    }
    

    So you pretty much put an object of one class and asign it to a field in your other class.

    for your edit

    set this line in your database class at the top.

    public connect; //add to dbclass
    

    Then do this in your function

    public function retrieve_item(){
        $connect = $this->db->connect; //added this
        $query = $connect->prepare("SELECT  * FROM posts"); //changed this
        $query->execute();
        $all_items = $query->fetchAll(PDO::FETCH_ASSOC);
        return $all_items;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁
  • ¥15 bywave配置文件写入失败
  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥15 Mabatis查询数据