Inheritance Problem/Bug

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!

Moderator: General Moderators

Post Reply
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Inheritance Problem/Bug

Post 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. :)
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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Inheritance Problem/Bug

Post by Weirdan »

does your Page have a constructor?
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Inheritance Problem/Bug

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Inheritance Problem/Bug

Post by Weirdan »

AlanG wrote:Yeah.
And you call parent::__construct(); inside of it, do you?
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Inheritance Problem/Bug

Post 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. :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Inheritance Problem/Bug

Post 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.
Post Reply