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?
Microsoft is reportedly seeking help from its biggest cloud rival, Amazon Web Services, to address mounting capacity issues of GitHub. According to a report by Business Insider, this move of the company comes after a series of AI-driven outages on the coding platform, which Microsoft acquired in 2018. Despites its plans to migrate GitHub completely to Azure by 2027, increasing demand from AI coding tools has forced Microsoft to adopt a multi-cloud strategy...............
https://cio.economictimes.indiatimes.com/news/corporate-news/microsoft-taps-aws-for-github-capacity-amid-ai-driven-outages-and-multi-cloud-strategy/131761981
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