The content introduced in this article is about [php classes and objects] objects and references, which has a certain reference value. Now I share it with everyone. Friends in need can refer to
Objects and References
PHP references are aliases, that is, two different variable names point to the same content.
In PHP 5, an object variable no longer holds the value of the entire object. Just save an identifier to access the real object content. When an object is passed as a parameter, returned as a result, or assigned to another variable, the other variable has no reference relationship with the original one, but they both store a copy of the same identifier, which points to the real content of the same object. .
Example #1 引用和对象$b->foo = 2;echo $a->foo."\n";$c = new A;$d = &$c; // $c ,$d是引用 // ($c,$d) =$d->foo = 2;echo $c->foo."\n";$e = new A;function foo($obj) { // ($obj) = ($e) = $obj->foo = 2; } foo($e);echo $e->foo."\n";?>
User Contributed Notes
/* Notes on reference: A reference is not a pointer. However, an object handle IS a pointer. Example: */Copy after login
Related recommendations:
[php classes and objects]trait
[php classes and objects]Final keyword
[php classes and objects]late static binding
The above is the detailed content of [php classes and objects] objects and references. For more information, please follow other related articles on the PHP Chinese website!