views:

144

answers:

2

So that count($object) will return the number of records in it

+7  A: 

If you have Standard PHP Library installed you should be able to simply implement Countable in your class and then define the count() function:

class foo implements Countable {
    ...
    public function count() {
        # do stuff here
        return $count;
    }
}

Read more about the SPL here: http://www.php.net/manual/en/book.spl.php

More about the Countable interface here: http://php.net/manual/en/countable.count.php

thetaiko
+3  A: 

Take a look at Countable::count

class MyClass implements Countable {
    public function count() {
        //return count
    }
}

$c = new MyClass();
count($c); //calls $c->count();
Yacoby