[thelist] PHP syntax problem

Hans Zaunere zaunere at yahoo.com
Tue Apr 16 21:57:01 CDT 2002


>
> When an instance of the Customer class is created you want one object
> referred to via $customer, not two objects. So:
>
> $customer =& new CustomerClass();
>
> Tells CustomerClass to build a new customer object, and set $customer
> to
> refer to it. Without the & you'll have two separate objects floating
> about: the one created by CustomerClass, and the one copied to
> $customer.

With PHP4 I don't think this is actually the case, making the =&
useless in this case, AFAIK.  In fact, testing has shown the excessive
reference use can be slower in the modern Zend engine.

>
> So much for the theory -- in practise I can't seem to get php to do
> anything of the sort if there's no &. Unless the garbage collector is
> incredibly efficient and destroying objects which can't be addressed
> as
> soon as they're generated, I'm not quite sure what's happening.

To the best of my knowledge, after talking with some people, I have
come to use references only sparingly.  Basically, this is the rule:
you don't need references unless you need to change the original
variable.  AFAIK, PHP4 uses reference counting and "copy-on-demand".
This means that if you pass a 40mb string into a function, but only
read from the string, no copy is done.  Only if you change the passed
in variable does any copy happen, unless you use a reference, which you
will then be editing the original string.  There are some additional
questions that I've thought of about this, however it has served me
well thus far, and topical testing has proved this to be true.

One day I'll read the code,

Hans Z.
New York PHP
http://nyphp.org



__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/



More information about the thelist mailing list