The first is a SQL class basically creating an object that will open a new sql connection and run queries.
class sqlConnection {
public $connection;
public function __construct($host, $username, $password){
// Setup new connection and save it in the $connection variable.
}
}
My next two classes are where it gets kind of tricky- we'll call them class Mommy and Daughter... Daughter is an extension of Mommy. Daughter also has child objects of its self within its self.
class Mommy {
public $_GLOBAL = array("dbconnection" => array(),
"somevariable" => "value");
public function __construct($variable_value){
$this->_GLOBAL["somevariable"] = $variable_value;
}
public function setDBConnection($id, $connection){
$this->_GLOBAL["dbconnection"][$id] = $connection;
}
public function getDBConnection($id){
return $this->_GLOBAL["dbconnection"][$id];
}
}
class Daughter extends Mommy {
public $child = array();
public function __construct(){
// This code creates multiple Daughter children within themselves which get mounted onto the $child array.
}
}
The Daughter class is also where the sqlConnection class gets created and used. However, the sqlConnection object gets mounted on the Mommy $_GLOBAL["dbconnection"] array and is supposed to act as a "Global" database connection for all children within the Daughter class. This is to prevent myself from having to redefine the sqlDatabase class for every child object within the Daughter class.
My problem is that when I define a new Daughter child it carries all of the Mommy variables with it, with the exception of the database connection that I want. If I run a print_r on my root Daughter object it looks kind of like this
As you can see- the "somevariable" variable got passed down to all of the children but the "dbconnection" variable didnt for some reason. Can anyone help me with this?
Question
tainted
Ok so I have a couple classes that Ive written.
The first is a SQL class basically creating an object that will open a new sql connection and run queries.
class sqlConnection { public $connection; public function __construct($host, $username, $password){ // Setup new connection and save it in the $connection variable. } }My next two classes are where it gets kind of tricky- we'll call them class Mommy and Daughter... Daughter is an extension of Mommy. Daughter also has child objects of its self within its self.
class Mommy { public $_GLOBAL = array("dbconnection" => array(), "somevariable" => "value"); public function __construct($variable_value){ $this->_GLOBAL["somevariable"] = $variable_value; } public function setDBConnection($id, $connection){ $this->_GLOBAL["dbconnection"][$id] = $connection; } public function getDBConnection($id){ return $this->_GLOBAL["dbconnection"][$id]; } } class Daughter extends Mommy { public $child = array(); public function __construct(){ // This code creates multiple Daughter children within themselves which get mounted onto the $child array. } }The Daughter class is also where the sqlConnection class gets created and used. However, the sqlConnection object gets mounted on the Mommy $_GLOBAL["dbconnection"] array and is supposed to act as a "Global" database connection for all children within the Daughter class. This is to prevent myself from having to redefine the sqlDatabase class for every child object within the Daughter class.
My problem is that when I define a new Daughter child it carries all of the Mommy variables with it, with the exception of the database connection that I want. If I run a print_r on my root Daughter object it looks kind of like this
As you can see- the "somevariable" variable got passed down to all of the children but the "dbconnection" variable didnt for some reason. Can anyone help me with this?
Link to comment
https://www.neowin.net/forum/topic/666382-php-parentchild-object-relations/Share on other sites
4 answers to this question
Recommended Posts