Re: question of Object
| From: | Tom Rogers | Date: | Tue, 20 May 2003 03:18:59 +0000 |
| Subject: | Re: question of Object | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-148243@lists.php.net to get a copy of this message | ||
Hi,
Tuesday, May 20, 2003, 8:25:11 AM, you wrote:
YL> Hi, i have a good question on objects.
YL> I have the fallowing classes Page, Elements,Menu,Product , Var (containt
YL> the post, get, session...) , Db and Error
YL> Page is created once, and i beleive that Db should be created once too.
YL> But at the moment it is created in every class that needs to execute a
YL> query.
YL> I dont find it too nice to do aggregation inside of every class. So i
YL> would like to put this Class on a higher level.
YL> Avisouly i dont want to do Db to extend all the classes because it would
YL> not be to pretty either.
YL> Plus using globals,... hummm
YL> So is there a way to do what i am looking for without using globals and
YL> having Db extend every class that do a connection to the Database ?
YL> Thanks
YL> Yann Larrivee
I use one global array of class references and do a lookup to see if
the class is already registered. Another way would be to setup the db
class and pass the reference to all the other classes.
Example (untested)
class aClass{
var $db;
function aClass(&$db){
$this->db =& $db;
}
}
$db =& new dbclass();
$a = new aClass($db);
--
regards,
Tom