RE: [PHP] - References Clarification Please -
| From: | Jason Lotito | Date: | Sun, 09 Dec 2001 19:04:26 +0000 |
| Subject: | RE: [PHP] - References Clarification Please - | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-77160@lists.php.net to get a copy of this message | ||
Actually, $this->db will be a copy of $dbObj.
Here is what you have:
<?php
$db = function2createDBObject();
Site($db);
function Site(&$dbObj)
{
$this->db = $dbObj;
}
?>
What this means is that $dbObj is a reference to $db. However,
$this->db is a copy. If you want $this->db to be a reference of $db,
you have to create a reference to $dbObj.
function Site(&$dbObj)
{
$this->db =& $dbObj;
}
Like that.
Jason Lotito
jason@lehighweb.com
www.NewbieNetwork.net
> -----Original Message-----
> From: Matt Friedman [mailto:matt@sprynewmedia.com]
> Sent: Sunday, December 09, 2001 1:34 PM
> To: php-general@lists.php.net
> Subject: [PHP] - References Clarification Please -
>
>
> Hi,
>
> I'm just trying to get my head around references and the uses
> etc... I have reviewed the manual section on this a few times
> but I'm still a bit fuzzy on it.
>
> First of all, here's some code I'm using to pass a reference
> to an object (which has global scope) to the constructor of a
> class "Site":
>
> function Site(&$dbObj)
> {
> $this->db = $dbObj;
> }
>
> I think what should happen here is that $this->db is now a
> reference to the global version of the object, by way of the
> "&" in the function defn. Is this correct?
>
> Also, I'm not sure about the benefits of using references as
> opposed to copies. If anyone would like to elaborate on the
> reasons for using a reference instead of a copy, please do
> so; it would be much appreciated.
>
>
> Many Thanks,
> Matt Friedman
>
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail:
> php-general-help@lists.php.net To contact the list
> administrators, e-mail: php-list-admin@lists.php.net
>