[thelist] PHP syntax problem

Andrew Forsberg andrew at thepander.co.nz
Tue Apr 16 21:07:01 CDT 2002


On Wed, 2002-04-17 at 12:03, Andy Warwick wrote:

> But I can't for the life of me work out why the class is being called in that
> way, and what benefits it creates. AFAICT it's something to do with copying
> objects, saving memory and the $this variable, but I can't find an easy example
> of why it's needed.

Hi Andy,

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.

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.

Cheers
Andrew





More information about the thelist mailing list