|
Posted by Matthew Weier O'Phinney on 10/21/73 11:19
* Jason Barnett <jason.barnett@telesuite.com> :
> Matthew Weier O'Phinney wrote:
> ...
> > This doesn't demonstrate what the OP was talking about, which is initial
> > assignment of an object using a reference operator. The results of this
> > make perfect sense to me -- the references are passed exactly as I would
> > expect.
>
> But not exactly as I would expect! I thought that all objects were
> created as references, but apparently this is not the case.
Here's what happens (in PHP5) with the statement '$obj = new Class();':
* the 'new Class()' construct creates an object
* it then returns a reference to the object
* assignment is then to the reference
Thus, $obj is actually a *pointer* to the object -- not the actual
object.
> Is there some kind of dereferencing going on when you're using an
> object on the right side of the assignment (=) operator?
No. It's simpler: you're simply passing around a reference to the
object.
Before, in PHP4, when you would use assignment, the variable on the left
side would receive a *copy* of the object on the right -- a clone of the
object. *UNLESS* what you had on the right side was a reference; then it
would clone the reference, meaning you have the same behaviour as in
PHP5.
This is why the $obj =& new Class() idiom occurred in PHP4; that way you
could pass around your object safely, because you were simply passing
around a reference to an object. PHP5 simplifies this by simply
assigning the object reference only in the first place.
--
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
[Back to original message]
|