这是我的问题,我在方法中使用静态变量。我使用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
谢谢你的帮助!