PHP5对象是否通过引用传递? [英] Are PHP5 objects passed by reference?

查看:70
本文介绍了PHP5对象是否通过引用传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't seem to get any consistent info on this. Different sources appear to say different things and the venerable php.net itself (appears) not to explicitly state this - although, I must admit, I only had a quick look.

如果我要传递重"对象,则需要通过引用传递,但我不想继续输入:

In cases where I am passing around 'heavy' objects, I need to pass by reference, but I don't want to keep typing:

function foo(TypeName& $obj)

如果我可以简单地逃脱

function foo(TypeName $obj)

那么标准怎么说?

推荐答案

对象通过引用传递(并分配).无需使用操作员的地址.

Objects are passed (and assigned) by reference. No need to use address of operator.

虽然我键入的内容过分简单,但可以满足您的目的. 文档指出:

Granted what I typed is an oversimplification but will suit your purposes. The documentation states:

PHP5 OOP的关键点之一 人们经常提到的是,对象 默认情况下通过引用传递". 这不是完全正确的.这 本节纠正了一般思想 用一些例子.

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.

PHP参考是别名,它是 允许两个不同的变量 写入相同的值.从PHP5开始, 对象变量不包含 对象本身就不再是价值.它 只包含一个对象标识符 允许对象访问者找到 实际的对象.当一个物体是 通过参数发送,返回或分配 到另一个变量,不同 变量不是别名:它们包含一个 标识符的副本,该副本指向 到同一个对象.

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.

有关更详细的解释(解释了过度简化和标识符),请查看

For a more detailed explanation (explains the oversimplification as well as identifiers) check out this answer.

这篇关于PHP5对象是否通过引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆