Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP函数,按值返回还是按引用返回?_Php - Fatal编程技术网

PHP函数,按值返回还是按引用返回?

PHP函数,按值返回还是按引用返回?,php,Php,在PHP中使用return语句时,结果是通过值返回还是通过引用返回 谢谢!Andree.在PHP中,默认情况下,所有内容都是通过值返回的(我肯定有例外,但我想不出任何atm)。除了默认通过引用传递的对象(PHP>5.0)之外。显然,它是通过引用返回的。这个简单的代码证明了这一点 <?php class InsideObject { public $variable; } class OutsideObject { private $insideObject;

在PHP中使用return语句时,结果是通过值返回还是通过引用返回


谢谢!Andree.

在PHP中,默认情况下,所有内容都是通过值返回的(我肯定有例外,但我想不出任何atm)。除了默认通过引用传递的对象(PHP>5.0)之外。

显然,它是通过引用返回的。这个简单的代码证明了这一点

<?php

class InsideObject
{
    public $variable;
}

class OutsideObject
{
    private $insideObject;

    public function __construct()
    {
        $this->insideObject = new InsideObject();
        $this->insideObject->variable = '1';
    }

    public function echoVar()
    {
        echo $this->insideObject->variable;
    }

    public function getInsideObject()
    {
        return $this->insideObject;
    }
}

$object = new OutsideObject();
$object->echoVar(); // should be 1

$insideObject = $object->getInsideObject();
$insideObject->variable = '2';

$object->echoVar(); // should be 2
在这种情况下,“it”是什么意思?提示您的问题的代码是否返回了对象?