On 17 April 2008 11:57, Bojan Tesanovic advised:

> in PHP5 by default Objects are passed by reference

Please stop repeating this -- erm -- inexactitude.

In PHP5, objects are passed around by their handle, *not* as a
reference. Most of the time, this has the same effect, as you are
addressing the same object either way. However, the behaviour of a
copied object handle is not the same as a reference. In fact, when you
create a reference to an object, what you're actually getting is a
reference to the object's handle!

To prove this, try running the test code below -- the object handling
will not, I assure you, behave as though you were passing a reference
around by default, but is directly comparable to the following string
example:

<?php

        class test {

                public $me;
        }

        $t = new test;
        $t->me = 'Original';

        $copy_t = $t;
        $ref_t = &$t;

        $copy_t = new test;
        $copy_t->me = 'Altered Copy';

        echo <<<RESULT1
        Original: $t->me<br />
        Copy: $copy_t->me<br />
        Reference: $ref_t->me<br />
RESULT1;

        $ref_t = new test;
        $ref_t->me = 'Altered Reference';

        echo <<<RESULT2
        <br />
        Original: $t->me<br />
        Copy: $copy_t->me<br />
        Reference: $ref_t->me<br />
RESULT2;


        $s = 'String';

        $copy_s = $s;
        $ref_s = &$s;

        $copy_s = 'String Copy';

        echo <<<RESULT3
        <br />
        Original: $s<br />
        Copy: $copy_s<br />
        Reference: $ref_s<br />
RESULT3;

        $ref_s = 'String Reference';

        echo <<<RESULT4
        <br />
        Original: $s<br />
        Copy: $copy_s<br />
        Reference: $ref_s<br />
RESULT4;

?>

Cheers!

 --
Mike Ford,  Electronic Information Services Adviser,
JG125, The Headingley Library,
James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 812 4730          Fax:  +44 113 812 3211


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to