Andi Gutmans wrote:
Why can't people who implement the Countable interface use $obj->count() instead of count($obj)?

I guess the idea of SPL is hide the difference between real arrays and some homebrewed containers. The question is how much you want to maintain this illusion, i.e. what operations you think are so frequent that they should be treated as worth overloading.


I was thinking along the lines of a DB abstraction layer which behaves like an array:

$a = new DBArray;
echo "Name: " . $a[$id]->Name; # Fetch ID $id and return field Name
$a[$id]->Name = "John Doe"; # Update record $id's Name

$b = new DBArray("Name LIKE "John %");
echo "We know " . count($b) . " Johns:";
foreach ($b as $john)
        echo " " . $john->Name;

It would work very well with $b->count() too, it just destroys the illusion of the DB result set being an array. And making count() work would sort of enforce a standard for returning the count of such a pseudo-array.

I'm not sure if that's worth the trouble though because this would (predictably) lead to people asking for other array functions working as well.

Luckily for me it's up to you (not me) to draw the line how far you want to go with SPL :-)

- Chris

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to