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