【问题标题】:Call to a member function on null?在 null 上调用成员函数?
【发布时间】:2017-08-09 09:14:33
【问题描述】:

我已经做了各种各样的事情来了解代码的实际问题,但我就是不知道是什么导致了这个错误。

下面是名为Topic 的类:

class Topic {

        // Init DB variables
        private $db;

        /*
        *   Constructor
        */
        public function __contruct() {
            $this->db = new Database();

        }

        /*
        *   Get All Topics
        */
        public function getAllTopics() {

            $this->db->query("SELECT topics.*, users.username, 
                              users.avatar, categories.name 
                              FROM topics 
                              INNER JOIN users 
                              ON topics.user_id = users.id 
                              INNER JOIN categories 
                              ON topics.category_id = categories.id 
                              ORDER BY create_date DESC");


            // Assign the results
            $results = $this->db->resultset();

            return $results;
        }
    }

当我创建一个 Topic 类的对象时,

$topic = new Topic();
$results = $topic->getAllTopics();

总是有这样的错误:

致命错误:在 null 上调用成员函数 query() /Applications/XAMPP/xamppfiles/htdocs/forumproject/libraries/Topic.php 在第 25 行,即 $this->db->query(...)

Database 类运行良好,因为我已经为该类独立运行了测试,并且没有问题。

这是数据库类:

<?php 

    /*
    * Using PDO for the first time in my life.
    */

    class Database  {

        private $host = DB_HOST;
        private $user = DB_USER;
        private $pass = DB_PASS;
        private $dbname = DB_NAME;

        private $dbh;
        private $error;
        private $stmt;

        public function __construct()   {
            // Set DSN
            $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;

            // Set options
            $options = array(
                PDO::ATTR_PERSISTENT => true, 
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            );

            // Create a new PDO instance
            try { 
                $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
            }
            // Catch the errors using exceptions handler
            catch(PDOException $e){
                $this->error = $e->getMessage();
            }
        }

        public function query($query){
            $this->stmt = $this->dbh->prepare($query);
        }

        public function bind($param, $value, $type = null){
            if(isnull($type)){
                switch(true){
                    case is_int($value):
                        $type = PDO::PARAM_INT;
                        break;

                    case is_bool($value):
                        $type = PDO::PARAM_BOOL;
                        break;

                    case is_null($value):
                        $type = PDO::PARAM_NULL;
                        break;

                    default : 
                        $type = PDO::PARAM_STR;
                }
            }

            $this->stmt->bindValue($param, $value, $type);
        }


        public function execute(){
            return $this->stmt->execute();
        }

        public function resultset(){
            $this->execute();
            return $this->stmt->fetchAll(PDO::FETCH_OBJ);
        }

        public function single(){
            $this->execute();
            return $this->stmt->fetch(PDO::FETCH_OBJ);
        }

        public function rowCount(){
            return $this->stmt->rowCount();
        }

        public function lastInsertId(){
            return $this->dbh->lastInsertId();
        }

        public function beginTransaction(){
            return $this->dbh->beginTransaction();
        }

        public function endTransaction(){
            return $this->bbh->commit();
        }

        public function cancelTransaction(){
            return $this->dbh->rollBack();
        }


    }

?>

【问题讨论】:

  • Database 类是否在同一个文件中?如果没有,你必须include/require它。
  • 这是您完整的主题文件吗?第 25 行出现致命错误提示,第 19 行调用的查询函数在哪里
  • @TomUdding 是的,我包括了它。
  • 是的。我使用了 var_dump($this->db),它返回 NULL,即使我使用 include 包含了数据库文件。
  • 能否也添加Database文件?

标签: php mysql pdo


【解决方案1】:

public function __contruct() 中有错字,应该是public function __construct() {(你少了一个 s)

【讨论】:

  • 我无法形容我现在的感受。我真的很惭愧,另一方面我真的很感谢你。自过去 3 小时以来,我一直坚持这一点。
  • 别担心,这种情况经常发生;)
  • 如果答案是:“你拼错了一些东西”,那么解决问题的适当方法是将其关闭为 Off-topic Typo
猜你喜欢
  • 2020-03-10
  • 2017-09-24
  • 2018-02-06
  • 2018-09-29
  • 2018-08-04
  • 2020-03-28
  • 2020-10-28
  • 2020-09-01
  • 2023-03-14
相关资源
最近更新 更多