AlexGrim Posted January 9, 2009 Share Posted January 9, 2009 Does anyone know of a reason that two different classes that extend the same parent class, and call the same method, behave differently when called from the same script? For example, i have a script, and you do something, and the script says "if this works, then call $class_a and $do_this(), else call $class_b and $do_this()", and both classes inherit from $class_c. But when you $do_this() with $class_b, it will say that $class_c could not fetch $class_b. I have an autoload function to load my classes, and all classes are called from the same level/scope, so i'm kinda stumped. If i call parent::__construct() in $class_b just before i call $do_this(), everything is fine... but if i do not, it will say that $class_c could not find $class_b, but $class_b is the one who is extending $class_c, so that doesn't make any sense either... Thanx. Link to comment https://forums.phpfreaks.com/topic/140114-solved-parent__construct-needed-for-one-class-but-not-the-other/ Share on other sites More sharing options...
btherl Posted January 9, 2009 Share Posted January 9, 2009 Can you cut out unrelated code and post a simple example demonstrating the problem? Or you can post the whole thing if it's not too big. Link to comment https://forums.phpfreaks.com/topic/140114-solved-parent__construct-needed-for-one-class-but-not-the-other/#findComment-733079 Share on other sites More sharing options...
AlexGrim Posted January 9, 2009 Author Share Posted January 9, 2009 <?php class MySQL extends MySQLi{ function __construct(){ $h="my server"; $u="my user"; $p="my pwd"; $d="my db"; parent::__construct($this->h,$this->u,$this->p,$this->d); } function Q($query){ //this prepares the query, and the other functions in this class will retrieve the data and do the proper preparations to be used as variables.... } //more functions... } class Account extends MySQL{ function CheckUser($a,$b,$c){ $r = parent::Q("call SP_Authenticate('{$a}','{$b}','{$c}')"); if ($r === true){ return true; }else{ return false; } } //more functions... } class Bug extends MySQL{ function __construct($desc){ //code to gather data about the bug... parent::Q("call SP_BugReport('{$desc}','{$etc}','{$etc}')"); } // NO more functions in this class... } ?> <?php //page script... $a = new Account(); if ($a->CheckUser($a,$b,$c) !== true){ //send them to the login page... } $i = new Image(); if ($i->DoSomethingWithThisImage() !== true){ $b = new Bug("Could not DoSomethingWithThisImage !"); //This is where i get the error //"Warning, MySQLi.Query(): Could not fetch Bug, in MySQL, on line 222". //But i do not call my Bug class from my MySQL class, //it is called the other way around. //Otherwise, if there really is an error in my MySQL class, //then it would call the bug, which uses mysql, so it gets caught in a deadlock. } ?> Link to comment https://forums.phpfreaks.com/topic/140114-solved-parent__construct-needed-for-one-class-but-not-the-other/#findComment-733111 Share on other sites More sharing options...
vbnullchar Posted January 9, 2009 Share Posted January 9, 2009 i think this is similar to your code. classB __construct overwrites the classA <?php class classA { function __construct(){ echo 'class a'; } } class classB extends classA { function __construct(){ echo 'class b'; } } $o = new classB(); ?> Link to comment https://forums.phpfreaks.com/topic/140114-solved-parent__construct-needed-for-one-class-but-not-the-other/#findComment-733163 Share on other sites More sharing options...
DarkSuperHero Posted January 9, 2009 Share Posted January 9, 2009 try changing <?php parent::Q("call SP_BugReport('{$desc}','{$etc}','{$etc}')"); //into (substitute parent with class name) MySQL::Q("call SP_BugReport('{$desc}','{$etc}','{$etc}')"); Link to comment https://forums.phpfreaks.com/topic/140114-solved-parent__construct-needed-for-one-class-but-not-the-other/#findComment-733302 Share on other sites More sharing options...
DarkSuperHero Posted January 9, 2009 Share Posted January 9, 2009 OR...try it like this... :-) <?php class MySQL extends MySQLi{ function __construct(){ $h="my server"; $u="my user"; $p="my pwd"; $d="my db"; //parent::__construct($this->h,$this->u,$this->p,$this->d); } function Q($query){ //this prepares the query, and the other functions in this class will retrieve the data and do the proper preparations to be used as variables.... return true; } //more functions... } class Account{ private $_handle; function __construct(MySQL $handle){ $this->$_handle = $handle; //you have a MySQL object inside your Account object, now you can call any function inside MySQL :-) } function CheckUser($a,$b,$c){ $r = $this->_handle->Q("call SP_Authenticate('{$a}','{$b}','{$c}')"); //calling Q menthod from inside MySQL class if ($r === true){ return true; }else{ return false; } } //more functions... } class Bug extends MySQL{ private $_handle function __construct(MySQL $handle,$desc){ //code to gather data about the bug... $this->_handle = $handle; $this->_handle->Q("call SP_BugReport('{$desc}'"); } // NO more functions in this class... } ?> <?php //page script... $a = new Account(new MySQL); if ($a->CheckUser($a,$b,$c) !== true){ //send them to the login page... } $i = new Image(); if ($i->DoSomethingWithThisImage() !== true){ $b = new Bug(new MySQL,"Could not DoSomethingWithThisImage !"); PS: I am still trying to get my head all the way around working with OO PHP...so please if anyone finds something bad or just plain out wrong...please do tell Link to comment https://forums.phpfreaks.com/topic/140114-solved-parent__construct-needed-for-one-class-but-not-the-other/#findComment-733328 Share on other sites More sharing options...
DarkSuperHero Posted January 9, 2009 Share Posted January 9, 2009 also note....i did not mean to change anything inside the MySQL class... Link to comment https://forums.phpfreaks.com/topic/140114-solved-parent__construct-needed-for-one-class-but-not-the-other/#findComment-733377 Share on other sites More sharing options...
AlexGrim Posted January 9, 2009 Author Share Posted January 9, 2009 __construct(MySQL $handle,$desc) Do you have a pointer to any documentation where i can find this? I have not seen that type of casting before, or whatever that is called, and i would like to research it. Also, all of my functions return *something*, whether it be a boolean or an array. I think that it may have something to do with using the construct of the bug class to do something. Here in a few minutes i am going to try putting the bug report into a separate function in the bug class, and see what happens... Thanx Link to comment https://forums.phpfreaks.com/topic/140114-solved-parent__construct-needed-for-one-class-but-not-the-other/#findComment-733700 Share on other sites More sharing options...
AlexGrim Posted January 9, 2009 Author Share Posted January 9, 2009 Fixed. In the bug class, i changed function __construct to function Report and in the script i changed $b=new Bug("test bug"); to $b=new Bug(); $b->Report("test bug"); and all is well. It seems that for some reason, when you try to call a parent method before the constructor is finished, then you must also call the parent class's construct as well. So by not using the parent class's methods in your construct (which in my case was not even necessary to begin with, and i just did that for the sake of brevity), you do not have to call the parent class's construct at all. Thanx guys. Link to comment https://forums.phpfreaks.com/topic/140114-solved-parent__construct-needed-for-one-class-but-not-the-other/#findComment-733710 Share on other sites More sharing options...
DarkSuperHero Posted January 9, 2009 Share Posted January 9, 2009 very nice explanation! :-) and to answer your question its something i pieced together from reading this on PHPFreaks.... http://www.phpfreaks.com/tutorial/design-patterns---strategy-and-bridge/page2 http://www.phpfreaks.com/tutorial/design-patterns---value-object/page2 theres probably others.... anyways... cheers! congrats on solving your issue :-) Link to comment https://forums.phpfreaks.com/topic/140114-solved-parent__construct-needed-for-one-class-but-not-the-other/#findComment-733738 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.