Page 1 of 1

Inheritance Problem/Bug

Posted: Wed Jun 10, 2009 1:09 am
by AlanG
Hey,

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.

Code: Select all

Fatal error: Call to a member function real_escape_string() on a non-object
So here's some example code.

Code: Select all

 
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. :)

Re: Inheritance Problem/Bug

Posted: Wed Jun 10, 2009 1:22 am
by Weirdan
does your Page have a constructor?

Re: Inheritance Problem/Bug

Posted: Wed Jun 10, 2009 1:07 pm
by AlanG
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.

Re: Inheritance Problem/Bug

Posted: Wed Jun 10, 2009 3:32 pm
by Weirdan
AlanG wrote:Yeah.
And you call parent::__construct(); inside of it, do you?

Re: Inheritance Problem/Bug

Posted: Wed Jun 10, 2009 3:46 pm
by AlanG
Weirdan wrote:
AlanG wrote:Yeah.
And you call parent::__construct(); inside of it, do you?
No... I didn't do that.

I have it like this:

Code: Select all

 
$system = new System();
$page = new Page('Home');
 
(I solved the problem by creating a new $database variable in Page and in the constructor I put: $this->database = parent::database;)

Nevermind, stupid question. Obviously the child class's constructor is overwriting the parent's? Yeah makes perfect sense. Thanks for the help. :)

Re: Inheritance Problem/Bug

Posted: Wed Jun 10, 2009 5:44 pm
by Weirdan
AlanG wrote:Obviously the child class's constructor is overwriting the parent's? Yeah makes perfect sense.
Yeah, it does. So usually people do it like this when extending constructor:

Code: Select all

 
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.