Re: Best way to use other objects within classes?

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

 



* "Murray @ PlanetThoughtful" <lists@xxxxxxxxxxxxxxxxxxxx>:
<snip>
> (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)
<snip>
> <?
>   $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;
> 	}

The above is the nut you need, and you're doing it right. As Greg
pointed out, you'll need to change that from 'private $db' to 'var $db'
as PHP4 doesn't have member visibility. However, you're correctly
passing by reference -- something required by PHP4 if you want to share
the same object amongst several classes. (In PHP5, objects are passed by
reference by default, and the above code will actually generate some
notices -- but since you're developing for PHP4, you can safely ignore
them).

-- 
Matthew Weier O'Phinney           | WEBSITES:
Webmaster and IT Specialist       | http://www.garden.org
National Gardening Association    | http://www.kidsgardening.com
802-863-5251 x156                 | http://nationalgardenmonth.org
mailto:matthew@xxxxxxxxxx         | http://vermontbotanical.org

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