Gor*_*don 13
正确的方法是实现Countable接口
<?php
class myCounter implements Countable {
public function count() {
static $count = 0;
return ++$count;
}
}
$counter = new myCounter;
for($i=0; $i<10; ++$i) {
echo "I have been count()ed " . count($counter) . " times\n";
}
Run Code Online (Sandbox Code Playgroud)
换句话说,你实现了自己count()应该返回的逻辑.