Best way to use other objects within classes?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi All,

I'm using the MDB2 object to access my MySQL database. I have a number of classes that perform page-building activities that need db access, and I'm wondering what the best way to expose the MDB2 object to them is?

(Note: my development environment is PHP 5.0.3, but the production environment is 4.3.10. This is my first project built with 5.x local and 4.1.x remote, so if anyone with more experience spots any fatal flaws where I'm using 5.x specific methods etc, I'd appreciate knowing about them)

Currently, I'm instantiating the MDB2 class in my main page, then passing it as an object reference to each of the other classes that require it.

A simple representation of my main page and a class would be:

<?
 $db =& connectMDB2(); // function that returns instantiated MDB2 object

$comments = new displayComments(); // class that performs displaying of comments
 $comments->set_db($db); // passing the MDB2 object to the class
 $comments->doSomethingElse();
?>

The displayComments() class might then look something like:

class displayComments{
	private $db;

	public function set_db(&$db){
		$this->db =& $db;
	}

	public function doSomethingElse(){
		$sql = "SELECT something FROM sometable";
		$rs = $this->db->query($sql);
		while ($row = $rs->fetchRow(MDB2_FETCHMODE_OBJECT)){
			echo $row->something."<br />";
		}
		$rs->free();
	}	
}

My main page calls on at least 8 or 9 such classes as it is being built, and I'm concerned that I may not be handling the MDB2 object in respect to them in the most efficient way possible. In a way, I guess this isn't specifically about the MDB2 package, but more about how to handle objects when they are required within classes.

I'd very much appreciate any comments or advice anyone might be able to give.

Much warmth,

Murray

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux