I have this class:
class template {
private $_db;
public function __construct(){
$this->_db = \MysqliDb::getInstance();
}
public function get(){
return $this->_db->rawQuery("SHOW COLUMNS FROM rank LIKE 'view_template'");
}
}
But, when I execute the method get
, I get this error message:
Fatal error: Call to a member function rawQuery() on a non-object in {dir/to/my/file} on line 50
Line 50 is return $this->_db->rawQuery("SHOW COLUMNS FROM rank LIKE 'view_template'");
The weird thing is that it works fine if I move the code from __construct
to the get
method, like this:
class template {
private $_db;
public function get(){
$this->_db = \MysqliDb::getInstance();
return $this->_db->rawQuery("SHOW COLUMNS FROM rank LIKE 'view_template'");
}
}
What can be wrong in this case?
I think
\MysqliDb::getInstance()
; isn't returning a valid object at the time when the construct is called, this is why you are getting the error.
You could check the object returned in the construct to see if a valid object is returned before calling the get method
public function __construct(){ $this->_db = \MysqliDb::getInstance(); var_dump( $this->_db); }