【问题标题】:PDO DB Instance returns null [closed]PDO 数据库实例返回 null [关闭]
【发布时间】:2014-01-19 16:59:28
【问题描述】:

我正在开发类似网站的任务管理器,但我的数据库对象 (PDO)

所以我有一个 数据库类 (DB),它有一个函数 getInstance(Singleton)。当我使用 User 类中的函数 create 创建用户时,这非常有效。

然后我创建了一个具有相同函数 create 的 Task 类,它也使用了 DB 类中的 getInstance 函数。但是后来我得到了这个错误

   Call to a member function insert() on a non-object

函数insert是Database类中的一个函数,用于查询数据库并插入数据。我在两个类的create函数中都使用了函数insert。

任务类

class Task {

    private $id, $task, $message, $date, $active, $finished, $user_id;
    public $_db;

    public function __contstruct() {
        $this->_db = DB::getInstance();
    }

    public function create($fields = array()) {
        if (!$this->_db->insert('tasks', $fields)) {
            throw new Exception('There was a problem creating an account');
        }
    }

}

非常感谢!

【问题讨论】:

    标签: php mysql pdo null


    【解决方案1】:

    您在__contstruct 中犯了语法错误。改为:

        public function __construct(){
    

    当然,Task 类的构造函数不会导致 => $_db 是空的。

    附:真的,阴险的错误:)

    【讨论】:

    • 哇,我很惭愧!感谢任何方式:D
    猜你喜欢
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 2019-05-24
    • 1970-01-01
    • 2022-01-02
    相关资源
    最近更新 更多