Jump to content

[SOLVED] parent::__construct needed for one class, but not the other?


AlexGrim

Recommended Posts

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

__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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.