php中对象的传递其实是值传递
https://www.tutorialspoint.com/how-do-you-pass-objects-by-reference-in-php-5
参考: https://www.cnblogs.com/aademeng/articles/9194688.html
This isn't "pass by reference". It actually is "assignment by reference". In PHP 5 assignment by reference is the default behaviour with objects.
A PHP reference is an alias, that allows two different variables to write it to the same value. In PHP version 5, an object variable doesn't contain the object itself as its value. It holds an object identifier that allows object accessors to find the actual object.
When an object is sent by an argument, returned or assigned to a different variable, these different variables are not aliases. They contain a copy of the identifier, that points to the same object
-----------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php class A { public $name = 'aa' ; public $age = 13; public $b = null; } class B { public $name = 'aa' ; } $a = new A(); $b = $a ; $b = 123; var_dump( $a ); var_dump( $b ); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步