• 0

[PHP] Parent/Child object relations


Question

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

Daughter Object (
	[child] => Array (
		[emily] => Daughter Object (
			[child] => Array(...)
			[_GLOBAL] => Array(
				[somevariable] => value
				[dbconnection] => Array()
				)
			)
		)
		[amy] => Daughter Object (
			[child] => Array(...)
			[_GLOBAL] => Array(
				[somevariable] => value
				[dbconnection] => Array()
				)
			)
		)
	)
	[_GLOBAL] => Array(
		[somevariable] => value
		[dbconnection] => Array(
			[] => sqlConnection Object(
				[connection] => Resource id #6
			)
		)
	)
)

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

  • 0

The mysql_connect() function in PHP returns the currently established database link, if there is one with the same host/user/pass already established, otherwise it creates a new one.

All you need to do is create a database connection using "sqlConnection" in your "Daughter" constructor. You won't end up with multiple database connections, just one. You don't really need the "Mommy" class to achieve this either.

It's a bit weird that your "Daughter" constructor is returning instances of itself within an array. You should probably take a look at the PHP5 OO manual to get a better feel for OO and how you can use it to your advantage.

Edited by ziadoz
  • 0

Well its a template class and the daughter class parses through my code and creates a new instance of its self per every "child" object in the html.

It seems like the mysql connections dont get carried down through the child objects since its being defined somewhere else in the child process.

  • 0

I guess what you're trying to achieve is an inherited $connection variable (I figured you were more concerned about duplicate MySQL connections in my previous post). That's pretty simple to achieve:

<?php
class sqlConnection {

	protected $connection;

	private $host = 'localhost';
	private $user = 'root';
	private $pass = '';
	private $db = 'test';

	public function __construct(){
		$this->connection = mysql_connect($this->host, $this->user, $this->pass);
		mysql_select_db($this->db, $this->connection);
	}
}

class Daughter extends sqlConnection {
	public $child = array();

	public function __construct(){
		parent::__construct();
		// This code creates multiple Daughter children within themselves which get mounted onto the $child array.
	}

	public function test() {
		$query = mysql_query('SELECT COUNT(*) FROM test', $this->connection);
		return mysql_result($query, 0, 0);
	}
}

$d = new Daughter;
echo $d->test();
?>

In the code when Daughter is constructed it will also construct its parent (sqlConnection) and inherit the $connection attribute (holding the MySQL link). I've made $connection protected which means only the parent and any descendants (i.e. Daughter) can access it. Also I've removed the parameters from the sqlConnection constructor and made them private class attributes (meaning only sqlConnection can access them). You could pull the connection attributes (host, user, pass and db) from an external configuration file or something if necessary. The test() function will simply perform a query using the inherited $connection link and return the result.

Hopefully this helps you understand inheritance (and polymorphism) some more. :)

Edited by ziadoz
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

  • Recent Achievements

    • One Year In
      Console General earned a badge
      One Year In
    • One Year In
      Twozo Technologies earned a badge
      One Year In
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
    • Veteran
      branfont went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      510
    2. 2
      +Edouard
      200
    3. 3
      PsYcHoKiLLa
      131
    4. 4
      Steven P.
      89
    5. 5
      neufuse
      74
  • Tell a friend

    Love Neowin? Tell a friend!