Re: OOP and object references, not copies - how to do?
| From: | Rasmus Lerdorf | Date: | Fri, 27 Dec 2002 15:29:10 +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-129443@lists.php.net to get a copy of this message | ||
PHP can pass references to objects around exactly the same way you pass
references to normal variables.
-Rasmus
On Thu, 26 Dec 2002, Erik Franzén wrote:
> 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.
>
> Best regards
> Erik
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>