3 ms·
Before 2017-12-01, the documentation for count() said: > Returns the number of elements in array_or_countable. When the parameter is neither an array nor an ob
by tzs 5y ago
Before 2017-12-01, the documentation for count() said:
> Returns the number of elements in array_or_countable. When the parameter is neither an array nor an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned.
If the languages developers did not want programmers to use that behavior they should have said that it is undefined when the input is not an array or countable. When you document how something behaves in any particular case programmers are going to use it, especially in a case like this where the behavior is actually useful.
- pluc 5y agoDo you not think languages should evolve? Do you think the count() behaviour should never have changed because at some point the documentation said this? How do you propose languages deal with evolution if not graceful deprecation? This isn't Go or Rust that have been around for a few years, this is a 30 year old language that was designed for an entirely different web than what we have today and has had to reinvent itself multiple times. Let me know when Go and Rust deal with half of the problems PHP dealt with.
- hakre 5y agoIn case of count this is more likely not because of a different web but the changes we can see for stdClass, arrays and the object model. And give peace a chance, it is not always love on first sight when something unexpected happens.
- hakre 5y ago> When you document how something behaves in any particular case programmers are going to use it, especially in a case like this where the behavior is actually useful. This is true and also the reason of the extraordinary warning and the years-long phase-out. The old count is not really a sharp-shooter, however once we got accustomed to it, we all have our habits. I only once needed it, IIRC: function good_old_count(mixed $mixed): int { return (is_array($mixed) || ($mixed instanceof Countable)) ? count($mixed) : ( (int) ( null !== $mixed ) ) ; }