Jump to content

Child class construct not running when class is extended...


5kyy8lu3

Recommended Posts

Hi.

 

I have a mysql class that I use to access my database with.  It's nothing crazy, just some simple methods with error handling.

 

I wanted to play around with extending a class, so I made a new class and extended my mysql class.

 

Bam, errors of "mysql parameter 1 needs to be mysqli" that sort of error, meaning i didn't have a connection.  makes sense, since my connection is set up in the __construct.

 

this leads me to believe that the child class construct only runs if i manually create an object of that class.  is there any way to run the construct?  would it be better for me to just create an object of that class within my parent class intead of extending it?  just seems to defeat the purpose of being able to extend a class if the construct doesn't run to set up my connection lol.

 

thanks =)

ok well I found another solution, but I'd still like some advice from a guru on the 'proper way' to go about this.

 

so i basically realized the construct of the child class won't run unless i manually run it or create an object from that class.  it won't run simply from extending the class.

 

so... i made an extra method in the child class with the same contents of the construct, and manually ran that method from within my parent class's construct.

 

there a better way to do this?  thanks =)

In the child class's __construct you call the parent's __construct.

 

public function __construct() {
  parent::__construct();
}

 

what I need is the exact opposite of that, the parent class's construct calling the child class's construct, but now that I know I can do that, that'll totally work.  thanks a bunch :D

 

EDIT: actually you were right, I think I was calling the parent/child backwards, but either way, solution found thanks to you, woot

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.