Php 如何将DB连接传递到另一个类?

Php 如何将DB连接传递到另一个类?,php,pdo,Php,Pdo,我当前正在尝试通过DB连接,如下所示: class Test { public $user; public $db; function __construct() { // connect to database try { $this->db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_DATABASE.'', DB_USERNAME, DB_PASSWORD

我当前正在尝试通过DB连接,如下所示:

class Test {
    public $user;
    public $db;

    function __construct() {
        // connect to database
        try {
            $this->db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_DATABASE.'', DB_USERNAME, DB_PASSWORD);
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch(PDOException $err) {
            die($err->getMessage());
        }
        $this->user = new User($this->db);
    }
}

class User {
    public $db;

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

    // execute some query
    $sql = "SELECT * FROM test";
    $sth = $this->db->prepare($sql);
    $sth->execute();
    $result = $sth->fetch();
    if(!empty($result)) {
        echo '<pre>';
        var_dump($result);
        echo '</pre>';
    }
}
类测试{
公共用户;
公帑$db;
函数_u构造(){
//连接到数据库
试一试{
$this->db=new-PDO('mysql:host='.db\u-host';dbname='.db\u-DATABASE'',db\u用户名,db\u密码);
$this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常);
}捕获(PDO异常$err){
死亡($err->getMessage());
}
$this->user=新用户($this->db);
}
}
类用户{
公帑$db;
函数构造($db){
$this->db=$db;
}
//执行一些查询
$sql=“从测试中选择*”;
$sth=$this->db->prepare($sql);
$sth->execute();
$result=$sth->fetch();
如果(!空($result)){
回声';
class User {
    public $db;

    function __construct($db) {
        if (! $db instanceof PDO) { die("What are you trying to pull anyway?"); }
        $this->db = $db;
    }

    function doSomething() {
        // execute some query
        $sql = "SELECT * FROM test";
        $sth = $this->db->prepare($sql);
        if ($sth === false) {
            die(print_r($this->db->errorInfo(), true));
        }
        $status = $sth->execute();
        if ($status === false) {
            die(print_r($sth->errorInfo(), true));
        }
        $result = $sth->fetch();
        if(!empty($result)) {
            echo '<pre>';
            var_dump($result);
            echo '</pre>';
        }
    }
}
var_dump($结果); 回声';
class User {
    public $db;

    function __construct($db) {
        if (! $db instanceof PDO) { die("What are you trying to pull anyway?"); }
        $this->db = $db;
    }

    function doSomething() {
        // execute some query
        $sql = "SELECT * FROM test";
        $sth = $this->db->prepare($sql);
        if ($sth === false) {
            die(print_r($this->db->errorInfo(), true));
        }
        $status = $sth->execute();
        if ($status === false) {
            die(print_r($sth->errorInfo(), true));
        }
        $result = $sth->fetch();
        if(!empty($result)) {
            echo '<pre>';
            var_dump($result);
            echo '</pre>';
        }
    }
}
} }

但我得到:致命错误:对非对象调用成员函数prepare()。我做错了什么?

您已经纠正了将db传递给类构造函数的部分

但这不是一个有效的类定义,就像您编写它的方式一样。您需要将这些代码行(在
//执行一些查询之后)放入函数中。代码行不能存在于它们所在的位置,在用户类中浮动,但不能在函数中浮动。这是不合法的

您还应该在每次调用prepare()或execute()后检查错误状态。如果出现错误(如SQL语法错误或表不存在等),它们将返回FALSE

类用户{
公帑$db;
函数构造($db){
如果(!$db instanceof PDO){die(“你到底想拉什么?”);}
$this->db=$db;
}
函数doSomething(){
//执行一些查询
$sql=“从测试中选择*”;
$sth=$this->db->prepare($sql);
如果($sth==false){
模具(打印($this->db->errorInfo(),true));
}
$status=$sth->execute();
如果($status==false){
模具(打印($sth->errorInfo(),true));
}
$result=$sth->fetch();
如果(!空($result)){
回声';
class User {
    public $db;

    function __construct($db) {
        if (! $db instanceof PDO) { die("What are you trying to pull anyway?"); }
        $this->db = $db;
    }

    function doSomething() {
        // execute some query
        $sql = "SELECT * FROM test";
        $sth = $this->db->prepare($sql);
        if ($sth === false) {
            die(print_r($this->db->errorInfo(), true));
        }
        $status = $sth->execute();
        if ($status === false) {
            die(print_r($sth->errorInfo(), true));
        }
        $result = $sth->fetch();
        if(!empty($result)) {
            echo '<pre>';
            var_dump($result);
            echo '</pre>';
        }
    }
}
var_dump($结果); 回声';
class User {
    public $db;

    function __construct($db) {
        if (! $db instanceof PDO) { die("What are you trying to pull anyway?"); }
        $this->db = $db;
    }

    function doSomething() {
        // execute some query
        $sql = "SELECT * FROM test";
        $sth = $this->db->prepare($sql);
        if ($sth === false) {
            die(print_r($this->db->errorInfo(), true));
        }
        $status = $sth->execute();
        if ($status === false) {
            die(print_r($sth->errorInfo(), true));
        }
        $result = $sth->fetch();
        if(!empty($result)) {
            echo '<pre>';
            var_dump($result);
            echo '</pre>';
        }
    }
}
} } }
您已将db传递给类构造函数的部分正确无误

但这不是一个有效的类定义,就像您编写它的方式一样。您需要将这些代码行(在
//执行一些查询之后)放入函数中。代码行不能存在于它们所在的位置,在用户类中浮动,但不能在函数中浮动。这是不合法的

您还应该在每次调用prepare()或execute()后检查错误状态。如果出现错误(如SQL语法错误或表不存在等),它们将返回FALSE

类用户{
公帑$db;
函数构造($db){
如果(!$db instanceof PDO){die(“你到底想拉什么?”);}
$this->db=$db;
}
函数doSomething(){
//执行一些查询
$sql=“从测试中选择*”;
$sth=$this->db->prepare($sql);
如果($sth==false){
模具(打印($this->db->errorInfo(),true));
}
$status=$sth->execute();
如果($status==false){
模具(打印($sth->errorInfo(),true));
}
$result=$sth->fetch();
如果(!空($result)){
回声';
class User {
    public $db;

    function __construct($db) {
        if (! $db instanceof PDO) { die("What are you trying to pull anyway?"); }
        $this->db = $db;
    }

    function doSomething() {
        // execute some query
        $sql = "SELECT * FROM test";
        $sth = $this->db->prepare($sql);
        if ($sth === false) {
            die(print_r($this->db->errorInfo(), true));
        }
        $status = $sth->execute();
        if ($status === false) {
            die(print_r($sth->errorInfo(), true));
        }
        $result = $sth->fetch();
        if(!empty($result)) {
            echo '<pre>';
            var_dump($result);
            echo '</pre>';
        }
    }
}
var_dump($结果); 回声';
class User {
    public $db;

    function __construct($db) {
        if (! $db instanceof PDO) { die("What are you trying to pull anyway?"); }
        $this->db = $db;
    }

    function doSomething() {
        // execute some query
        $sql = "SELECT * FROM test";
        $sth = $this->db->prepare($sql);
        if ($sth === false) {
            die(print_r($this->db->errorInfo(), true));
        }
        $status = $sth->execute();
        if ($status === false) {
            die(print_r($sth->errorInfo(), true));
        }
        $result = $sth->fetch();
        if(!empty($result)) {
            echo '<pre>';
            var_dump($result);
            echo '</pre>';
        }
    }
}
} } }
我很确定里面有语法错误:你的
}
太多了。你的“用户”计算器中有内联代码-一个类只能包含变量和函数定义。我很确定里面有语法错误:你的
}
太多了。你的“用户”计算器中有内联代码-一个类只能包含变量和函数定义。即使代码包装在函数中,我仍然会遇到相同的错误?我测试了这段代码,它工作正常。您确定错误不是来自应用程序的其他部分吗?另外,在用户构造函数中测试$db的有效性也是值得的。我将编辑上面的代码。@ThilankaDeSilva,为什么?即使代码包装在一个函数中,我仍然会得到相同的错误?我测试了这段代码,它工作正常。您确定错误不是来自应用程序的其他部分吗?另外,在用户构造函数中测试$db的有效性也是值得的。我会编辑上面的代码。@ThilankaDeSilva,为什么?