致命错误:未被捕获的错误:在null上调用成员函数query() [英] Fatal error: Uncaught Error: Call to a member function query() on null

查看:223
本文介绍了致命错误:未被捕获的错误:在null上调用成员函数query()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OOP的新手,仍在尝试中,有人可以指出我的错误在哪里吗? 我遇到错误

I'm a newbie to OOP and still experimenting, can someone point out where my error is? I'm getting error

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

Fatal error: Uncaught Error: Call to a member function query() on null on the following line:

我已经意识到数据库连接不是从Core类继承的,但是我不知道为什么.

I have realized that the DB connection is not being inherited from the Core class, but I have no idea why.

$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

My db variable is passed from my config.php file which I include at the beginning of the index.php page 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');

推荐答案

现在的问题在于调用类的方式.核心类__construct()函数需要$ db连接.现在,User类将必须调用Core类的构造函数来启动db连接.因此,您至少有一个选择:

The problem right now lies within the way your calling the classes. The Core class __construct() function expects the $db connection. Right now, the User class will have to call the constructor function of the Core class to initiate the db connection. So you have one option at least:

仅调用User类,如下所示:

Call only the User classes like this:

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

在更改用户构造函数以执行此操作时

while changing your User constructor to do this:

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

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

If your database connection variables are all correct (host/username/password/port), it should work.

这篇关于致命错误:未被捕获的错误:在null上调用成员函数query()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆