又发现一个PHP Feature,PHP成员函数中竟然也能用static声明变量。怎么想怎么奇怪,你都创建新实例了,怎么static还是有效?不过有时候还蛮有用的。
class static_var {
function __construct($name) {
$this->name = $name;
}
function output() {
static $counter;
$counter++;
echo $counter.'|'.$this->name.'|';
}
}
$t1 = new static_var('take 1');
$t1->output();
$t2 = new static_var('take 2');
$t2->output();
你好,程序猿