致命错误:未捕获错误:在null上调用成员函数query()

时间:2017-02-23 02:29:14

标签: php mysql class oop

我是OOP的新手并且还在尝试,有人可以指出我的错误在哪里吗? 我收到错误

  

致命错误:未捕获错误:在以下行上调用null上的成员函数query():

我已经意识到数据库连接没有从Core类继承,但我不明白为什么。

$user = $this->db->query("SELECT * FROM users WHERE id='".$user_id."' LIMIT 1");

class.php

class Core {
        public $db;
        public $domain;
        public $settings;
        public function getDomain() {
            return $this->domain;
        }
        function __construct($db,$domain) {
            $this->db = $db;
            $this->domain = $domain;
            $this->getSettings();
        }
        public function getSettings() {
            $settings = $this->db->query("SELECT * FROM settings");
            $settings = $settings->fetch_object();
            $this->settings = $settings;
        }
    }

    class User extends Core {
        public $user;
        function __construct($user_id) {
            $this->setUser($user_id);
        }
        public function setUser($user_id) {
            $user = $this->db->query("SELECT * FROM users WHERE id='".$user_id."' LIMIT 1");
            if($user->num_rows >= 1) {
                $user = $user->fetch_object();
                $this->user = $user;
            }
        }

的index.php

$core = new Core($db,$domain);
$user = new User($_SESSION['user_id']);

澄清:

我的db变量从我的config.php文件传递,该文件包含在index.php页面的开头 的的config.php

$_db['host'] = 'localhost';
$_db['user'] = 'root';
$_db['pass'] = 'root';
$_db['name'] = 'social';
$db = new mysqli($_db['host'], $_db['user'], $_db['pass'], $_db['name']) or die('MySQL Error');

3 个答案:

答案 0 :(得分:1)

如果有一个构造子类的方法,如果直接调用该方法,则不会使用父类的子类

class Core {
    public $db;
    public $domain;
    public $settings;
    public function getDomain() {
        return $this->domain;
    }
    function __construct($db,$domain) {
        $this->db = $db;
        $this->domain = $domain;
        $this->getSettings();
    }
    public function getSettings() {
        $settings = $this->db->query("SELECT * FROM settings");
        $settings = $settings->fetch_object();
        $this->settings = $settings;
    }
}

class User extends Core {
    public $user;
    function __construct($user_id,$db,$domain) {
        parent::__construct($db,$domain);
        $this->setUser($user_id);
    }
    public function setUser($user_id) {
        $user = $this->db->query("SELECT * FROM users WHERE id='".$user_id."' LIMIT 1");
        if($user->num_rows >= 1) {
            $user = $user->fetch_object();
            $this->user = $user;
        }
    }

并在index.php中只有$user = new User($_SESSION['user_id'],$db,$domain);

答案 1 :(得分:1)

现在的问题在于你调用类的方式。 Core类__construct()函数需要$ db连接。现在,User类必须调用Core类的构造函数来启动db连接。所以你至少有一个选择:

只调用这样的用户类:

$user = new User($db,$domain,$_SESSION['user_id']);

在更改User构造函数时执行此操作:

function __construct($db,$domain,$user_id) {
    parent::__construct($db,$domain);
    $this->setUser($user_id);
}

如果您的数据库连接变量都正确(主机/用户名/密码/端口),它应该可以工作。

答案 2 :(得分:0)

继承后没有设置父构造函数。

例如

class Core {
        public $db;
        public $domain;
        public $settings;
        public function getDomain() {
            return $this->domain;
        }
        function __construct($db,$domain) {
            $this->db = $db;
            $this->domain = $domain;
            $this->getSettings();
        }
        public function getSettings() {
            $settings = $this->db->query("SELECT * FROM settings");
            $settings = $settings->fetch_object();
            $this->settings = $settings;
        }
}

class User extends Core 
{
    public $user;

    // parent:param Parameters add 
    function __construct($user_id,$db,$domain) {
        // parent constructer call 
        parent::__construct($db,$domain);
        $this->setUser($user_id);
    }
    public function setUser($user_id) {
        $user = $this->db->query("SELECT * FROM users WHERE id='".$user_id."' LIMIT 1");
        if($user->num_rows >= 1) {
            $user = $user->fetch_object();
            $this->user = $user;
        }
    }
}


$user = new User($_SESSION['user_id'],$db,$domain);

这不行吗? http://php.net/manual/en/language.oop5.decon.php