5kyy8lu3 Posted March 15, 2010 Share Posted March 15, 2010 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 =) Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted March 15, 2010 Author Share Posted March 15, 2010 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 =) Quote Link to comment Share on other sites More sharing options...
trq Posted March 15, 2010 Share Posted March 15, 2010 In the child class's __construct you call the parent's __construct. public function __construct() { parent::__construct(); } Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted March 15, 2010 Author Share Posted March 15, 2010 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 EDIT: actually you were right, I think I was calling the parent/child backwards, but either way, solution found thanks to you, woot Quote Link to comment Share on other sites More sharing options...
trq Posted March 15, 2010 Share Posted March 15, 2010 what I need is the exact opposite of that, the parent class's construct calling the child class's construct A child inherits from its parent, not the other way around. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.