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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2715026/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:27:40  来源:igfitidea点击:

Are PHP5 objects passed by reference?

php

提问by morpheous

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.

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

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)

if I can get away with simply

如果我能逃脱

function foo(TypeName $obj)

So what does the standard say?

那么标准怎么说呢?

回答by webbiedave

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 documentationstates:

当然,我输入的内容过于简化,但适合您的目的。该文件指出:

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.

经常被提及的 PHP5 OOP 的关键点之一是“默认情况下通过引用传递对象”。这并不完全正确。本节使用一些示例纠正了这种一般性想法。

PHP 引用是一个别名,它允许两个不同的变量写入相同的值。从 PHP5 开始,对象变量不再包含对象本身作为值。它只包含一个对象标识符,它允许对象访问者找到实际的对象。当一个对象通过参数发送、返回或分配给另一个变量时,不同的变量不是别名:它们持有指向同一个对象的标识符的副本。

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

有关更详细的解释(解释过度简化以及标识符),请查看此答案

回答by Josh

From the PHP manual:

PHP 手册

You can pass a variable by reference to a function so the function can modify the variable. The syntax is as follows:

<?php
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
// $a is 6 here
?>

Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);.

您可以通过引用函数来传递变量,以便函数可以修改变量。语法如下:

<?php
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
// $a is 6 here
?>

注意:函数调用没有引用符号——仅在函数定义上。仅函数定义就足以通过引用正确传递参数。从 PHP 5.3.0 开始,当您在 foo(&$a); 中使用 & 时,您将收到一条警告,指出“调用时传递引用”已被弃用。

And from What's New in PHP5:

PHP5 中的新功能

In PHP 5, the infrastructure of the object model was rewritten to work with object handles. Unless you explicitly clone an object by using the clone keyword you will never create behind the scene duplicates of your objects. In PHP 5, there is neither a need to pass objects by reference nor assigning them by reference

在 PHP 5 中,对象模型的基础结构被重写以使用对象句柄。除非您使用 clone 关键字明确克隆对象,否则您永远不会在幕后创建对象的副本。在 PHP 5 中,既不需要通过引用传递对象,也不需要通过引用分配它们

So therefore the only time you need to use the function foo(&$var)syntax is if $var might not be an instance of a class.

因此,因此您唯一需要使用该function foo(&$var)语法的情况是 $var 可能不是类的实例。

回答by Will

It seems to be a little more precise, the value of an object is passed by value, but the value of an object itself is a pointer. This is different from just passing a reference.

好像更精确一点,对象的值是按值传递的,但是对象本身的值是一个指针。这与仅传递引用不同。

On http://www.php.net/manual/en/language.oop5.references.phpthe example listed is nice. In the first set, $a = NULL; doesn't affect $b since $a was just a pointer. In the second set, $c = NULL; causes $d to be NULL as well since $d is a reference to $c.

http://www.php.net/manual/en/language.oop5.references.php上列出的例子很好。在第一组中,$a = NULL; 不会影响 $b 因为 $a 只是一个指针。在第二组中,$c = NULL; 也导致 $d 为 NULL,因为 $d 是对 $c 的引用。

<?php
class A {
    public $foo = 1;
}  

$a = new A;
$b = $a;
$a->foo = 2;
$a = NULL;
echo $b->foo."\n"; // 2

$c = new A;
$d = &$c;
$c->foo = 2;
$c = NULL;
echo $d->foo."\n"; // Notice:  Trying to get property of non-object...
?> 

回答by Abraham Vegh

As of PHP 5, all objects are passed and assigned by reference.

从 PHP 5 开始,所有对象都通过引用传递和分配。

In PHP 4, you still need to specify where you want objects passed by reference, by explicitly using the &operator.

在 PHP 4 中,您仍然需要通过显式使用&运算符来指定希望通过引用传递对象的位置。

回答by AbcAeffchen

Just an example where passing "objects" by reference is useful:

只是一个通过引用传递“对象”很有用的例子:

class RefTest1
{
    public $foo;

    public function __construct(RefTest2 &$ref = null)
    {
        $this->foo =& $ref;
    }
}

class RefTest2
{
    public $foo;

    public function __construct(RefTest1 &$ref = null)
    {
        $this->foo =& $ref;
    }
}

class RefTest3
{
    public $foo;

    public function __construct(RefTest2 &$ref = null)
    {
        $this->foo =& $ref;
    }
}

class DoCrossRef
{
    public $refTest1;
    public $refTest2;

    public function __construct()
    {
        $this->refTest1 = new RefTest1($this->refTest2);
        $this->refTest2 = new RefTest2($this->refTest1);
    }

    public function changeReference()
    {
        $this->refTest1 = new RefTest3($this->refTest2);
    }
}

At the end RefTest1holds a reference to RefTest2and the other way around, also if the RefTest2object did not exist at the time RefTest1was created.

最后RefTest1持有对的引用,RefTest2反之亦然,如果RefTest2对象在RefTest1创建时不存在。

After calling DoCrossRef->changeReference(), the RefTest2objects also holds a reference to the new RefTest3object.

调用后DoCrossRef->changeReference()RefTest2对象还持有对新RefTest3对象的引用。

回答by álvaro González

Short answer is yes. From Objects and references:

简短的回答是肯定的。从对象和引用

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.

经常被提及的 PHP5 OOP 的关键点之一是“默认情况下通过引用传递对象”。这并不完全正确。本节使用一些示例纠正了这种一般性想法。

PHP 引用是一个别名,它允许两个不同的变量写入相同的值。从 PHP5 开始,对象变量不再包含对象本身作为值。它只包含一个对象标识符,它允许对象访问者找到实际的对象。当一个对象通过参数发送、返回或分配给另一个变量时,不同的变量不是别名:它们持有指向同一个对象的标识符的副本。

What matters is that, in the case that worries you, you'll never be making a copy of an object unless you explicitly use the clonekeyword in the function call. Whether it's an alias or an identifier doesn't change this fact.

重要的是,在您担心的情况下,除非您在函数调用中明确使用clone关键字,否则您永远不会复制对象。无论是别名还是标识符都不会改变这一事实。

回答by Sarfraz

Yes from PHP5 onwards, objects are passed by reference. No need to do that explicitly.

是的,从 PHP5 开始,对象是通过引用传递的。无需明确地这样做。

http://www.php.net/manual/en/migration5.oop.php

http://www.php.net/manual/en/migration5.oop.php

In PHP 5 there is a new Object Model. PHP's handling of objects has been completely rewritten, allowing for better performance and more features. In previous versions of PHP, objects were handled like primitive types (for instance integers and strings). The drawback of this method was that semantically the whole object was copied when a variable was assigned, or passed as a parameter to a method. In the new approach, objects are referenced by handle, and not by value (one can think of a handle as an object's identifier).

在 PHP 5 中有一个新的对象模型。PHP 对对象的处理已完全重写,从而提供更好的性能和更多功能。在以前的 PHP 版本中,对象像原始类型一样处理(例如整数和字符串)。这种方法的缺点是,在分配变量或作为参数传递给方法时,整个对象在语义上被复制。在新方法中,对象通过句柄而不是值来引用(可以将句柄视为对象的标识符)。

回答by Svetoslav Marinov

Here's another example confirming that objects are passed by reference.

这是另一个确认对象是通过引用传递的示例。

<?php

// discussion: https://stackoverflow.com/questions/2715026/are-php5-objects-passed-by-reference#_=_
class A {
    public $foo = 1;
}

class B {
    function inc($obj) {
        $obj->foo++;
    }
}

$objA = new A();
$objB = new B();
$objB->inc($objA);

echo "[" . $objA->foo . "]"; // Outputs [2]