Re: [RFC] Counting of non-countable objects
| From: | Nikita Popov | Date: | Tue, 04 Oct 2016 17:10:49 +0000 |
| Subject: | Re: [RFC] Counting of non-countable objects | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-96234@lists.php.net to get a copy of this message | ||
On Tue, Oct 4, 2016 at 12:29 PM, Craig Duncan <php@duncanc.co.uk> wrote:
> On 4 October 2016 at 11:17, Leigh <leight@gmail.com> wrote:
>
> > You specifically mention that counting scalars is unaffected, is there
> > a legitimate use-case for being able to use count() on them?
> >
> > I'd say using count() on a string or an int also constitutes a hidden
> > bug, as it also always returns 1 regardless of the value.
> >
>
> I agree, and I'm happy to include scalars in the RFC if that's desired by
> the majority.
>
> Calling count() on a scalar is likely to cause a warning at some point, so
> it's not as "hidden" as the iterator example:
>
> if (count($string) > 0) {
> foreach ($string as $value) {
>
A confounding factor is that count() has an alias sizeof() and for people
coming from a C-like background it is quite natural to try to apply
sizeof() to a string in order to get its length. This will silently "work",
but return a meaningless result.
So, definitely +1 on a warning for all non-array non-Countable cases.
Nikita