I'm a newbie to OOP and still experimenting, can someone point out where my error is?
I'm getting error
Fatal error: Uncaught Error: Call to a member function query() on null on the following line:
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']);
Clarification:
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');
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:
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.
If there is a method of constructing a subclass, the subclass of the parent class will not be used if the method is called directly
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;
}
}
and in index.php just$user = new User($_SESSION['user_id'],$db,$domain);
It is not set a parent constructor after inherit.
for example
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);
Is this not okay?
http://php.net/manual/en/language.oop5.decon.php