这是我的问题,我在方法中使用静态变量。我使用for
循环来创建新的实例。重新初始化新实例的静态变量
class test_for{
function staticplus(){
static $i=0;
$i++;
return $i;
}
function countplus() {
$res = '';
for($k=0 ; $k<3 ; $k++) {
$res .= $this->staticplus();
}
return $res;
}
}
for($j=0 ; $j<3 ; $j++) {
$countp = new test_for;
echo $countp->countplus().'</br>';
}
它返回:
123
456
789
有没有一种方法创建新实例时初始化静态变量,所以这是回报:
123
123
123
感谢您的帮助!
看来你所要求的是静态变量的对立面。相反,你想要一个实例变量。 – Dunes 2013-03-18 09:11:49