当你想将函数的返回引用绑定到某个变量时,PHP允许你这么做:
function &returns_reference()
{
static $someref = 0;
$someref++;
return $someref;
}
$newref = &returns_reference();//引用返回,相当于 $newref = &$someref;
echo $newref; //1
$notref = returns_reference(); //值传递的是副本
$newref = 100;
echo $notref; //2
$newref = 100;
echo returns_reference(); //101
class foo {
public $value = 0;
public function &getValue() {
return $this->value;
}
}
$obj = new foo;
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
$obj->value = 2;
echo $myValue;