6
這樣count($object)
將在返回的記錄數它如何在PHP中實現Countable接口?
這樣count($object)
將在返回的記錄數它如何在PHP中實現Countable接口?
class MyClass implements Countable {
public function count() {
//return count
}
}
$c = new MyClass();
count($c); //calls $c->count();
如果您有標準PHP庫安裝,你應該能夠簡單地實現你的類可數,然後定義count()函數:
class foo implements Countable {
...
public function count() {
# do stuff here
return $count;
}
}
瞭解更多關於這裏的SPL: http://www.php.net/manual/en/book.spl.php
更多關於可數的接口在這裏: http://php.net/manual/en/countable.count.php