gpt4 book ai didi

php - PHP5 对象是通过引用传递的吗?

转载 作者:IT老高 更新时间:2023-10-28 12:01:26 25 4
gpt4 key购买 nike

我似乎无法获得任何一致的信息。不同的来源似乎在说不同的东西,而古老的 php.net 本身(似乎)没有明确说明这一点 - 尽管我必须承认,我只是快速浏览了一下。

在我传递“重”对象的情况下,我需要通过引用传递,但我不想继续输入:

function foo(TypeName& $obj)

如果我能轻松逃脱

function foo(TypeName $obj)

那么标准是怎么说的呢?

最佳答案

对象通过引用传递(和分配)。无需使用运营商地址。

我输入的内容过于简单化,但符合您的目的。 documentation状态:

One of the key-points of PHP5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true. This section rectifies that general thought using some examples.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP5, 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.

如需更详细的解释(解释过度简化以及标识符),请查看 this answer .

关于php - PHP5 对象是通过引用传递的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2715026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com