Re: Best way to use other objects within classes?

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

 



Hi,

Friday, June 3, 2005, 5:16:19 AM, you wrote:
MP> Hi All,

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

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

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

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

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

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

MP> The displayComments() class might then look something like:

MP> class displayComments{
MP> 	private $db;

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

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

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

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

MP> Much warmth,

MP> Murray

My solution to this problem was to write a class loader and to call it
whenever I needed a particular class. I give each instance a name and
the loader checks if an instance of that class, that name exists. If
it does it returns a  reference directly, if not it loads the class
and then returns the reference. This way I don't have to worry about
passing references around. It is a little more complex than that as I
also check if the class was passed the same variables, if not it calls
the constructor again. Let me know if you want to try the code (only
tested on php4)

-- 
regards,
Tom

-- 
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