If objects are passed by alias, when do you follow the link?

Possible duplicate:
In PHP, can anyone explain the cloning of a pointer reference?

According to http://php.net/manual/en/language.oop5.references.php

One of the key points of PHP 5 OOP, which is often mentioned, is that "objects are passed by default by default." This is not entirely true. In this section, you can correct this general idea using some examples.

A PHP reference is an alias that allows two different variables to write the same value. Starting in PHP 5, the object variable no longer contains the object as a value. It contains only the object identifier, which allows object accessories to find the actual object. When an object is sent with an argument returned or assigned to another variable, the different variables are not aliases: they contain a copy of the identifier that points to the same object.

If objects are passed by an alias or handler, then in what situation do you really want to follow the link?

myFunc($obj); // Pass by alias/handler

myFunc(& $obj); // Pass by reference (when would you do this?)

myFunc(clone $obj); // Create a new object

What are the different use cases for each of them?

+5
source share
3 answers

What are the different use cases for each of them?

You have already named three different use cases:

  • Passing an object as a parameter

, , . 1. 99,9%. 0,1%, .

( ) (Demo):

class VarFreezer
{
    private $alias;
    private $value;
    public function __construct(&$object) {
        $this->alias = &$object;
        $this->value = $object;
    }
    public function restore()
    {
        $this->alias = $this->value;
    }
}

$var = new stdClass();
$var->value = 'I am an object now.';

$freezer = new VarFreezer($var);

$var = 'I am a string now.';

$freezer->restore();

var_dump($var);
+2

, , , .

, , . , .

.

& $obj $obj, , $obj. php.net:

$c = new A;
$d = &$c;
$c->foo = 2;
$c = NULL;
echo $d->foo."\n"; // Notice:  Trying to get property of non-object...
+2

myfunc ($ obj)//

- /.

myfunc (& $obj)// .

, . , ($ obj) null, , $obj, . , , , , , , $obj null, .

:

var x = new Object();

var y = x //y and x both contain **different** identifiers to the same object in memory.

function a($_a){} // a function for examples

a($y) // passes in a copy of $y so $_a (inside the function) is a third identifier pointing to th in memory object

a(& $y) // now $_a contains the **same** identifier as $y so if either is emptied then the other is also emptied. 

a(clone $y) //creates a new object identical to the first and passes in an identifier of the new object.

, .

you must pass the identifier by reference if you want to be able to change or delete the identifier inside the function and influence the variable of the external identifier, at the same time being able to return an independently calculated value. for example, serialize an object, save it in a file, return the line of the file, but also clear the identifier so that the object can be cleared from memory.

+2
source

All Articles