简体   繁体   中英

Php By Reference

Can someone please explain what the "&" does in the following:

class TEST {

}

$abc =& new TEST();

I know it is by reference. But can someone illustrate why and when I would need such a thing? Or point me to a url where this is explained well. I am unable to grasp the concept.

Thank you very much.

As I understand it, you're not asking about PHP references in general, but about the $foo =& new Bar(); construction idiom.

This is only seen in PHP4 as the usual $foo = new Bar() stores a copy of the object. This generally goes unnoticed unless the class stored a reference to $this in the constructor. When calling a method on the returned object later on, there would be two distinct copies of the object in existence when the intention was probably to have just one.

Consider this code where the constructor stores a reference to $this in a global var

class Bar {
    function Bar(){
       $GLOBALS['copy']=&$this;
        $this->str="hello";
    }

}

//store copy of constructed object
$x=new Bar;
$x->str="goodbye";

echo $copy->str."\n"; //hello
echo $x->str."\n"; //goodbye

//store reference to constructed object
$x=&new Bar;
$x->str="au revoir";

echo $copy->str."\n"; //au revoir
echo $x->str."\n"; //au revoir

In the first example, $x and $copy refer to different instances of Foo, but in the second they are the same.

Firstly, you don't really need to use it if you are using PHP 5, in PHP 5 all objects are passed by reference by default.

Secondly, when you assign an object to a variable name, either by creation, passing in a parameter, or setting a variable value, you are either doing so by reference or value.

Passing by reference means you pass the actual memory reference for the object, so say you passed an object as a parameter to a function, any changes that function makes to that variable will be reflected in the parent method as well, you are actually changing the state of that object in memory.

The alternative, to pass by value means you pass a copy of that object, not the memory reference, so any changes you make, will not be reflected in the original.

The PHP Manual does a pretty decent job of explaining references.

I should note, that they are NOT the same thing as a pointer or a reference in many other languages, although there are similarities. And as for objects being "passed by reference" by default - that's not exactly true either.

I would recommend reading the manual section first (and probably then re-reading a couple of times until you get it), and then come back here if you still have more questions.

A simpler way to look at it may be like this:

$a = 'foo';
$b = 'bar';
$a =& $b;
$b = 'foobar';
echo $a . ' ' . $b;

will output

foobar foobar

It might be helpful to think of it like this: In PHP, all variables are really some sort of pointer: The entries in the symbol table - the thing which maps variable names to values - contain a zval * in the C implementation of the Zend Engine.

During assignment - this includes setting function arguments - magic will happen:

If you do $a = $b , a copy of the value pointed to by the symbol table entry of $b will be created and a pointer to this new value will be placed in the symbol table entry for $a . Now, $a and $b will point to different values. PHP uses this as its default calling convention.

If you do $a =& $b , the symbol table entry for $a will be set to the pointer contained in the symbol table entry for $b . This means $a and $b now point to the same value - they are aliases of each other with equal rights until they are rebound by the programmer. Also note that $a is not really a reference to $b - they are both pointers to the same object.

That's why calling them 'aliases' might be a good idea to emphasize the differences to C++' reference implementation:

In C++, a variable containing a value and a reference created from this variable are not equal - that's the reason why there are things like dangling references.

To be clear: There is no thing like a reference type in PHP, as all variables are already internally implemented as pointers and therefore every one of them can act as a reference.

PHP5 objects are still consistent with this description - they are not passed by reference, but a pointer to the object (the manual calls it an 'object identifier' - it might not be implemented as an actual C pointer - I did not check this) is passed by value (meaning copied on assignment as described above).

Check the manual for details on the relation between PHP5 objects and references .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM