Objects and references in php 5

断了今生、忘了曾经 提交于 2021-02-08 07:44:56

问题


I found below lines on php.net manual which I couldn't understand what exactly they are trying to say.

"As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object. "

I couldn't understand what is diff. between reference and identifier in php5. They given below example.

class A {
    public $foo = 1;
}  

$a = new A;
$b = $a;     // $a and $b are copies of the same identifier
             // ($a) = ($b) = <id>
$b->foo = 2;
echo $a->foo."\n";


$c = new A;
$d = &$c;    // $c and $d are references
             // ($c,$d) = <id>

$d->foo = 2;
echo $c->foo."\n";


$e = new A;

function foo($obj) {
    // ($obj) = ($e) = <id>
    $obj->foo = 2;
}

foo($e);
echo $e->foo."\n";

//Outputs 2 2 2

It gives same output then when we should use ref. and we should copy object without ref?

I'll be grateful if anyone can explain me exact meaning of ref. and identifier.

Thanks, Jimit


回答1:


$a = new A;
$b = $a;
$b->foo = 'bar';  // changes the object that the identifier in $a and $b both refer to

$b = new B;       // overwrites $b with a new object identifier
$b->foo = 'baz';  // changes only $b's object

$a->foo // bar
$b->foo // baz

Contrast that with:

$a = new A;
$b = &$a;         // BIG DIFFERENCE HERE!
$b->foo = 'bar';  // changes the object that both $a and $b refer to

$b = new B;       // overwrites both $a and $b with a new object identifier
$b->foo = 'baz';

$a->foo // baz
$b->foo // baz

Variable references (&) work as they always work, you make two variables refer to the same value. Changing either variable changes both variables, they're more or less linked together.

The thing about objects being just identifiers just means that if you copy an object to another variable ($a = $b), both variables hold the same object identifier. But both variables are not linked in the same way they are when using & references.

An object identifier is pretty literally something that identifies an object. It's a value like the integer 42. It's a value that says "I'm referring to object #1". The actual object itself is stored elsewhere and fetched when needed. Object identifiers are one level of indirection:

$a = 42;
$b = &$a;

Here both variables refer to the exact same value. In the symbol table this looks like:

symbol | value
-------+------
a, b   | 42

But with object identifiers it looks like this:

$a = new A;
$b = $a;

symbol | value             object   | details
-------+---------          ---------+--------
a      | object#1          object#1 | class A { ... }
b      | object#1

(The tables above are mostly for illustrational purposes, actual technical details may differ.)



来源:https://stackoverflow.com/questions/19680283/objects-and-references-in-php-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!