在 PHP 5 中如何通过引用传递对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9331519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you pass objects by reference in PHP 5?
提问by Brian Graham
In PHP 5, are you required to use the &
modifier to pass by reference? For example,
在 PHP 5 中,您是否需要使用&
修饰符来按引用传递?例如,
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.
在 PHP4 中,您需要在&
更改后使用修饰符来维护引用,但我对我阅读的有关 PHP5 自动使用传递引用的主题感到困惑,除非明确克隆对象。
In PHP5, is the&
modifier required to pass by reference for all types of objects (variables, classes, arrays, ...)?
在 PHP5 中,&
所有类型的对象(变量、类、数组等)都需要通过引用传递修饰符吗?
回答by webbiedave
are you required to use the & modifier to pass-by-reference?
您是否需要使用 & 修饰符来传递引用?
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:
从技术上/语义上来说,答案是肯定的,即使是对象也是如此。这是因为有两种方法可以传递/分配对象:通过引用或通过标识符。当函数声明包含 时&
,如下所示:
function func(&$obj) {}
The argument will be passed by reference, no matter what. If you declare without the &
无论如何,参数将通过引用传递。如果你没有申报&
function func($obj) {}
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:
一切都将按值传递,对象和资源除外,然后将通过identifier传递。什么是标识符?好吧,您可以将其视为对引用的引用。以下面的例子为例:
class A
{
public $v = 1;
}
function change($obj)
{
$obj->v = 2;
}
function makezero($obj)
{
$obj = 0;
}
$a = new A();
change($a);
var_dump($a);
/*
output:
object(A)#1 (1) {
["v"]=>
int(2)
}
*/
makezero($a);
var_dump($a);
/*
output (same as before):
object(A)#1 (1) {
["v"]=>
int(2)
}
*/
So why doesn't $a
suddenly become an integer after passing it to makezero
? It's because we only overwrote the identifier. If we had passed by reference:
那么为什么$a
在将它传递给 后不会突然变成一个整数makezero
呢?这是因为我们只覆盖了identifier。如果我们通过引用传递:
function makezero(&$obj)
{
$obj = 0;
}
makezero($a);
var_dump($a);
/*
output:
int(0)
*/
Now $a
is an integer. So, there is a difference between passing via identifierand passing via reference.
现在$a
是一个整数。因此,通过identifier传递和通过reference传递是有区别的。
回答by SoWhat
You're using it wrong. The $ sign is compulsory for any variable. It should be: http://php.net/manual/en/language.references.pass.php
你用错了。$ 符号对于任何变量都是强制性的。它应该是:http: //php.net/manual/en/language.references.pass.php
function foo(&$a)
{
$a=null;
}
foo($a);
To return a reference, use
function &bar($a){
$a=5;
return $a
}
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.
在对象和数组中,对对象的引用被复制为形参,对两个对象的任何相等操作都是引用交换。
$a=new People();
$b=$a;//equivalent to &$b=&$a roughly. That is the address of $b is the same as that of $a
function goo($obj){
//$obj=$e(below) which essentially passes a reference of $e to $obj. For a basic datatype such as string, integer, bool, this would copy the value, but since equality between objects is anyways by references, this results in $obj as a reference to $e
}
$e=new People();
goo($e);
回答by Chris
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 &):
对于您拥有的代码(首先您需要 $ 甚至 & ):
$original = new Object();
one($original); //$original unaffected
two($original); //$original will now be null
function one($a) { $a = null; } //This one has no impact on your original variable, it will still point to the object
function two(&$a) { $a = null; ) //This one will set your original variable to null, you'll lose the reference to the object.