[thelist] PHP syntax problem

Andy Warwick mailing.lists at creed.co.uk
Wed Apr 17 04:25:01 CDT 2002


| On 2002/04/17, Andrew Forsberg said:

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

It looks like that may be the case. After a little more investiagtion I came
across a script at <http://imawebdesigner.com/ext-doc/assignbyref.phps> which
I've tweaked and presented here.

It seems that if you use the constructor of the class to create a reference to
the object itself as it is created - to stop it being destroyed - you can show
that there are two objects created, but that one is destroyed by the garbage
collector.

So, AFAIKT, although both ways you only end up with one object, creating an
instance without reference actually creates two then deletes one...

So I can only assume that creating an instance by reference *will* save you
memory and time.

Of course, I may be completely misunderstanding what is going on here...

----
<?php
class myClass
{
    var $datavar;

    function &myClass()
    {
        $GLOBALS['ref_to_myObj'] = & $this; // create a reference to this obj so
it doesn't get destroyed

        $this->datavar = "This is the data for the first object.";

        return $this;
    }
}

echo "<b>First try it without an assignment by reference...</b><br />\n<br
/>\n";

$myObj = new myClass();

echo "\$myObj & \$ref_to_myObj created...<br />\n";
echo "&#160;&#160;\$myObj->datavar = " . $myObj->datavar ."<br />\n";
echo "&#160;&#160;\$ref_to_myObj->datavar = " . $ref_to_myObj->datavar ."<br
/><br />\n";

$myObj->datavar = "This is the data for the assigned-without-reference myObj";

echo "\$myObj->datavar changed to 'This is the data for the
assigned-without-reference myObj'<br />\n";
echo "&#160;&#160;\$myObj->datavar = " . $myObj->datavar ."<br />\n";
echo "&#160;&#160;\$ref_to_myObj->datavar = " . $ref_to_myObj->datavar ."<br
/><br />\n";

echo "<b>Now trying it with an assignment by reference...</b><br />\n<br />\n";

unset($myObj);
unset($ref_to_myObj);

$myObj = &new myClass();

echo "\$myObj & \$ref_to_myObj created...<br />\n";
echo "&#160;&#160;\$myObj->datavar = " . $myObj->datavar ."<br />\n";
echo "&#160;&#160;\$ref_to_myObj->datavar = " . $ref_to_myObj->datavar ."<br
/><br />\n";

$myObj->datavar = "This is the data for the assigned-with-reference myObj";

echo "\$myObj->datavar changed to 'This is the data for the
assigned-with-reference myObj'<br />\n";
echo "&#160;&#160;\$myObj->datavar = " . $myObj->datavar . "<br />\n";
echo "&#160;&#160;\$ref_to_myObj->datavar = " . $ref_to_myObj->datavar ."<br
/><br />\n";
?>
----



More information about the thelist mailing list