You are here: Re: Best way to use other objects within classes? « PHP « IT news, forums, messages
Re: Best way to use other objects within classes?

Posted by Matthew Weier O'Phinney on 06/03/05 05:15

* "Murray @ PlanetThoughtful" <lists@planetthoughtful.org>:
<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@garden.org | http://vermontbotanical.org

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация