PHP 中的对象是按值传递还是按引用传递?

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

Are objects in PHP passed by value or reference?

phpoop

提问by Click Upvote

In this code:

在这段代码中:

<?php
class Foo
{
    var $value;

    function foo($value)
    {
        $this->setValue($value);
    }

    function setValue($value)
    {
        $this->value=$value;
    }
}

class Bar
{
    var $foos=array();

    function Bar()
    {
        for ($x=1; $x<=10; $x++)
        {
            $this->foos[$x]=new Foo("Foo # $x");
        }
    }

    function getFoo($index)
    {
        return $this->foos[$index];
    }

    function test()
    {
        $testFoo=$this->getFoo(5);
        $testFoo->setValue("My value has now changed");
    }
}
?>

When the method Bar::test()is run and it changes the value of foo # 5 in the array of foo objects, will the actual foo # 5 in the array be affected, or will the $testFoovariable be only a local variable which would cease to exist at the end of the function?

当该方法Bar::test()运行并更改 foo 对象数组中 foo #5 的值时,数组中的实际 foo #5 是否会受到影响,或者该$testFoo变量是否只是一个局部变量,最终将不复存在的功能?

回答by Jeremy Ruten

Why not run the function and find out?

为什么不运行该函数并找出答案?

$b = new Bar;
echo $b->getFoo(5)->value;
$b->test();
echo $b->getFoo(5)->value;

For me the above code (along with your code) produced this output:

对我来说,上面的代码(连同你的代码)产生了这个输出:

Foo #5
My value has now changed

This isn't due to "passing by reference", however, it is due to "assignment by reference". In PHP 5 assignment by reference is the default behaviour with objects. If you want to assign by value instead, use the clonekeyword.

这不是由于“通过引用传递”,而是由于“通过引用赋值”。在 PHP 5 中,通过引用赋值是对象的默认行为。如果要改为按值分配,请使用clone关键字。

回答by tomzx

You can refer to http://ca2.php.net/manual/en/language.oop5.references.phpfor the actual answer to your question.

您可以参考http://ca2.php.net/manual/en/language.oop5.references.php以获得您问题的实际答案。

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.

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 开始,对象变量不再包含对象本身作为值。它只包含一个对象标识符,它允许对象访问者找到实际的对象。当一个对象通过参数发送、返回或分配给另一个变量时,不同的变量不是别名:它们持有指向同一个对象的标识符的副本。

回答by Emil H

They are passed by value in PHP 4 and by reference in PHP 5. In order to pass objects by reference in PHP 4 you have to explicitly mark them as such:

它们在 PHP 4 中通过值传递,在 PHP 5 中通过引用传递。 为了在 PHP 4 中通过引用传递对象,您必须显式标记它们:

$obj = &new MyObj;