昨天我和我的一个朋友正在浏览静态类和实例类;然后突然发生了一些奇怪的行为。
<?php
class NumberContainerO {
public $_number;
public function __construct($number) {
$this->_number = $number;
}
}
$list = array();
$n = new NumberContainerO(1);
$list[] = &$n->_number;
$n = new NumberContainerO(2);
$list[] = &$n->_number;
$n = new NumberContainerO(3);
$list[] = &$n->_number;
var_dump($list);
?>
<?php
class NumberContainer {
public static $_number;
public static function __Add($number) {
self::$_number = $number;
}
}
$list = array();
NumberContainer::__Add(1);
$list[] = &NumberContainer::$_number;
NumberContainer::__Add(2);
$list[] = &NumberContainer::$_number;
NumberContainer::__Add(3);
$list[] = &NumberContainer::$_number;
var_dump($list);
?>
输出
数组(3) { [0]=> int(1) [1]=> int(2) [2]=> &int(3) } 数组(3) { [0]=> &int(3) [1] => &int(3) [2]=> &int(3) }
为什么 int(1) 和 int(2) 不通过引用传递?