Re: OOP and object references, not copies - how to do?

From: Date: Sun, 23 Feb 2003 12:19:41 +0000
Subject: Re: OOP and object references, not copies - how to do?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-136719@lists.php.net to get a copy of this message
erik@nordicaudi.com (Erik FranzéN) wrote in news:20021226175700.10738.qmail@pb1.pair.com: > Say the you are going to create a simple forum and you want to have a > number of classes: > > Class Sql - handles the DB interface > Class User - handles users > Class Messages - handles messages > > When you are writing the code, you first creates a new sql object in > order to read or write data from or to the database. > > Secondly you create a user object in order to handle users, for an > example, to authenticate the user which is going to write a new > message in your forum. > > The user object have a method which reads user information from the > database. > Since you alread have created a sql object, this method should use > the sql > object in order to fecth data from the database. > > I nice way to do this, would be to send the sql object as an reference > to the user object constructor and store the object reference to the > sql object in the user object. > > This is not working very well in PHP yet because PHP cannot treat > objects as references. When you send the sql object to the user object > constructor method, it will be a copy, not a reference! > > How do I get around this? There must be a way to handle this and > create nice OOO PHP-code? I don't like using global variables as > object handles. > You need to use the & chracter when passing & assigning. $obj =& new Object; function func(&$obj) { } To return an object: function &func() { return $obj; }

« previous php.general (#136719) next »