你可以设置引用返回的变量的值,无论是 `static` 函数变量还是对象的 `private` 属性(这非常危险 o.o)。
静态函数变量
<?php
function &func(){
static $static = 0;
return $static;
}
$var1 =& func();
echo "var1:", $var1, "\n"; func();
$var1 = 90;
echo "var1:", $var1, "\n"; echo "static:", func(), "\n"; ?>
私有属性
<?php
class foo {
private $value = 1;
public function &getValue() {
return $this->value;
}
public function setValue($val) {
$this->value = $val;
}
}
$obj = new foo;
$myValue = &$obj->getValue(); echo $obj->getValue(); echo $myValue; $obj->setValue(5);
echo $obj->getValue(); echo $myValue; $myValue = 1000;
echo $obj->getValue(); echo $myValue; ?>