PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Could someone confirm if the problem is with my logic or not.
I have a class called System. One of it's attributes is an object called Database (which extends MySQLi). On a child class of System (called Page), I want to use this Database object. But I keep getting a fatal error.
class System
{
protected $database;
public function __construct()
{
// ... Variables are included here
$this->database = new Database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
}
}
class Database extends MySQLi
{
// yada yada
}
class Page extends System
{
private function get_page($title)
{
$this->database->real_escape_string($title); // [color=#FF0000]This line causes the error[/color]
}
}
Any ideas on why this is happening? Thanks for any attempts.
Last edited by Benjamin on Wed Jun 10, 2009 1:40 pm, edited 1 time in total.
Reason:Changed code type from text to php.
Yeah. I'm just thinking out loud, but when the System class is instanciated, it assigns a Database object to it's database attribute, but when the Page class is instanciated, the System object already exists. So there should be no problem then?
What is puzzling, is that the error dissappears when I remove the inhertiance from the Database class (extends MySQLi). The workaround is to overwrite the functions, but I don't want to have to do that. Just makes for irrelevant code. It seems as if the inheritance is conflicting, but I don't believe that should be happening.
class A {
public function __construct($arg1, $arg2) {
$this->arg1 = $arg1;
$this->arg2 = $arg2;
}
}
class B extends A {
public function __construct($arg1, $arg2, $arg3) {
parent::__construct($arg1, $arg2);
$this->arg3 = $arg3;
}
}
This way ensures no parent behavior is lost and when parent construct routine is changed it automatically affect all child classes.