在 PHP 5 中如何通过引用传递对象?
在 PHP 5 中,是否需要使用 &
修饰符来按引用传递?例如,
class People() { }
$p = new People();
function one($a) { $a = null; }
function two(&$a) { $a = null; )
在 PHP4 中,您需要 &
修饰符在进行更改后维护引用,但我对我读过的有关 PHP5 自动使用引用传递的主题感到困惑,除非显式克隆对象。
在 PHP5 中,是否需要 &
修饰符来通过引用传递所有类型的对象(变量、类、数组……)?
In PHP 5, are you required to use the &
modifier to pass by reference? For example,
class People() { }
$p = new People();
function one($a) { $a = null; }
function two(&$a) { $a = null; )
In PHP4 you needed the &
modifier to maintain reference after a change had been made, but I'm confused on the topics I have read regarding PHP5's automatic use of pass-by-reference, except when explicity cloning the object.
In PHP5, is the &
modifier required to pass by reference for all types of objects (variables, classes, arrays, ...)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从技术/语义上讲,答案是是,即使对于对象也是如此。这是因为有两种方法可以传递/分配对象:通过引用或通过标识符。当函数声明包含
&
时,如下所示:无论如何,参数都将通过引用传递。如果您在没有
&
的情况下进行声明,则所有内容都将按值传递,但对象和资源除外,它们将通过标识符传递。什么是标识符?嗯,你可以把它看作是对参考的参考。举个例子:
那么为什么
$a
在传递给makezero
之后没有突然变成一个整数呢?这是因为我们只覆盖了标识符。如果我们通过引用传递:现在
$a
是一个整数。因此,通过标识符传递和通过引用传递之间存在差异。Technically/semantically, the answer is yes, even with objects. This is because there are two ways to pass/assign an object: by reference or by identifier. When a function declaration contains an
&
, as in:The argument will be passed by reference, no matter what. If you declare without the
&
Everything will be passed by value, with the exception of objects and resources, which will then be passed via identifier. What's an identifier? Well, you can think of it as a reference to a reference. Take the following example:
So why doesn't
$a
suddenly become an integer after passing it tomakezero
? It's because we only overwrote the identifier. If we had passed by reference:Now
$a
is an integer. So, there is a difference between passing via identifier and passing via reference.对象将按引用传递。内置类型将按值传递(复制);
幕后发生的事情是,当您传入保存对象的变量时,它是对该对象的引用。因此变量本身被复制,但它仍然引用同一个对象。因此,本质上有两个变量,但两者都指向同一个对象。对函数内对象所做的更改将持续存在。
对于您那里的代码(首先您需要 $ 即使带有 &):
Objects will pass-by-reference. Built in types will be pass-by-value (copied);
What is happening behind the scenes is that when you pass in a variable that holds an object, it's a reference to the object. So the variable itself is copied, but it still references the same object. So, essentially there are two variable, but both are pointing to the same object. Changes made to objects inside a function will persist.
In the case of the code that you have there (first you need $ even with &):
你用错了。 $ 符号对于任何变量都是必需的。应该是:
http://php.net/manual/en/language.references.pass.php
在对象和数组中,对象的引用被复制为形式参数,两个对象上的任何相等操作都是引用交换。
You're using it wrong. The $ sign is compulsory for any variable. It should be:
http://php.net/manual/en/language.references.pass.php
In objects and arrays, a reference to the object is copied as the formal parameter, any equality operations on two objects is a reference exchange.