[php classes and objects] objects and references

不言
Release: 2023-03-24 09:38:02
Original
2617 people have browsed it

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";?>
Copy after login

User Contributed Notes

/* Notes on reference: A reference is not a pointer. However, an object handle IS a pointer. Example: */
        
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!