【问题标题】:How to write dependency injection for database connection mysqli in php?如何在php中为数据库连接mysqli编写依赖注入?
【发布时间】:2018-11-26 12:07:58
【问题描述】:

我试图写写依赖注入,但类文件出错。如何正确编写数据库连接类并将其用作依赖注入?请检查以下错误。如何编写一次连接并在 php 文件中到处调用它。

数据库.php

class Database
{
    private $host ="localhost";
    private $user = "root";
    private $password="xxxx";
    private $db="";
    private $mysqli;


    function __construct($host,$user,$pass,$data) {

        $this->host     = $host;
        $this->user     = $user;
        $this->pass     = $pass;
        $this->data     = $data;
        $this->mysqli   = new mysqli($this->host, $this->user, $this->pass, $this->data);
    }

    public function query($query)
    {
        return $this->mysqli->query($query);
    }
}

Dummy.php

require_once("../apitest/database.php");

class Dummy
{
    protected $db;

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

    function get_test_yada(){
        return $this->db->mysqli->query("SELECT test FROM test")->fetch_object()->test;
    }
}

代码:

$test = new Dummy();
echo $test->get_test_yada();

错误

PHP 致命错误:未捕获的 TypeError:参数 1 传递给 Dummy::__construct() 必须是数据库的实例,没有给出, 在第 19 行调用 /var/www/html/apitest/index.php 并在 /var/www/html/apitest/index.php:8\n堆栈跟踪:\n#0 /var/www/html/apitest/index.php(19): Dummy->__construct()\n#1 {main}\n 在第 8 行的 /var/www/html/apitest/index.php 中抛出

【问题讨论】:

  • 你有什么错误?
  • 在哪里写入配置详细信息
  • 您似乎忘记在 Dummy 类中声明 $db 变量。
  • @OlegNurutdinov [2018 年 11 月 26 日星期一 17:57:25.636338] [:error] [pid 13751] [client ::1:40700] PHP 致命错误:未捕获的 TypeError:参数 1 传递给 Dummy: :__construct() 必须是 Database 的一个实例,没有给出,在第 19 行的 /var/www/html/apitest/index.php 中调用并在 /var/www/html/apitest/index.php:8\nStack 中定义跟踪:\n#0 /var/www/html/apitest/index.php(19): Dummy->__construct()\n#1 {main}\n 在 /var/www/html/apitest/index 中抛出。 php 在第 8 行

标签: php


【解决方案1】:

当您尝试实例化 Dummy 对象时,您没有传递数据库实例。

应该是这样的:

<?php

$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'db';

$db = new Database($host,$user,$pass,$dbname);
$test = new Dummy($db);
echo $test->get_test_yada();

【讨论】:

  • 如何一次实例化为依赖注入并随处使用
  • 这取决于您的应用程序架构,您可以通过多种方式实现。最简单的方法是使 $db 变量成为全局变量。
  • 但这就是你应该如何实例化它的方式。您应该创建新票的所有其他事情,我们将为您提供适当的答案。
【解决方案2】:

这个错误意味着你需要将Database object作为参数传递给你的Dummy对象

传递给 Dummy::__construct() 的参数 1 必须是 数据库

我认为你对依赖注入的工作原理有错误的想法,我强烈建议你在继续之前阅读一些关于它的文章。

Wikipedia Dependency Injection

Simple tutorial about Dependency Injection in tutplus

Dependency Injection (DI) Container in PHP来自 Medium 的酷文章

PHP-DI 一个php依赖注入容器包

【讨论】:

    【解决方案3】:

    $data$db 的属性在哪里?

    class Database
    {
        private $host ="localhost";
        private $user = "root";
        private $password="xxx";
        private $db="";
        private $mysqli;
    
    
        function __construct($host,$user,$pass,$data) {
    
            $this->host     = $host;
            $this->user     = $user;
            $this->pass     = $pass;
            $this->db       = $data;
            $this->mysqli   = new mysqli($this->host, $this->user, $this->pass, $this->db);
        }
    }
    
    class Dummy 
    {
        protected $db;
    
        function __construct(Database $db)
        {
           $this->db = $db;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 2017-08-20
      • 2020-08-30
      • 2018-12-30
      • 2016-11-19
      • 2016-08-22
      相关资源
      最近更新 更多