[thelist] PHP syntax problem

Andrew Forsberg andrew at thepander.co.nz
Tue Apr 16 23:27:00 CDT 2002


On Wed, 2002-04-17 at 14:57, Hans Zaunere wrote:

> > $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.

Lol! Perhaps someone should tell the php documentation project about
this? I guess it may make porting to 'real' OOP languages easier if & is
used explicitly, but it's funny that it has no real functional value and
may have a negative performance payload.

Also, I'm slightly annoyed that the zend engine second guesses
developers by assuming they won't follow the documentation's
recommendations.

> > 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".

So the copy only happens when an object (that wasn't created by
reference) is modified?

The simple tests I ran before responding to the original post created an
object by reference, and another object without the &. The default
constructor created a number and stored it in a var via $this. Then a
second method was called which unset the same var. Dumping $GLOBALS
before and after the unset() showed two objects only, never a third.

I just ran a variant of that where the object's variable is altered
rather than unset -- dumping $GLOBALS still doesn't show up a third
object. Have you been able to get two objects by using this syntax?:

$foo = new FooBarClass();

I can't make it do anything of the sort.

If $foo is a copy, not a reference, the object it is a copy of couldn't
have a name -- which is why I was dumping $GLOBALS to try to find it. If
it doesn't have a name (assuming there's no default / random naming
scheme for this sort of thing) then how can it exist?

But I think I've gone around in at least a couple of circles here, so if
there's any further info you have on the subject I'd really like to hear
it.

> One day I'll read the code,

Ugh! Whether it's C or C++ I don't think I'm going to have the free time
this side of retirement to learn it... :-)


Cheers
Andrew





More information about the thelist mailing list