# [RFC] Counting of non-countable objects

- Thread: https://externals.io/message/96211
- Original archive: https://news-web.php.net/php.internals/96211
- Started by Craig Duncan on 2016-10-04T09:32:34+00:00
- Messages: 13
- RFC: Counting of non-countable objects (Implemented (PHP 7.2)) — https://externals.io/rfc/counting_non_countables.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #96211 — Craig Duncan — 2016-10-04T09:32:34+00:00

URL: https://externals.io/message/96211#96211

Hi everybody

I'd like to propose the introduction of warning when counting objects that
can't be counted.

The default behaviour is to return 1 for these objects, which can be
misleading and hide bugs when attempting to count iterable objects (eg
Generators). Adding a warning would alert developers to the issue

https://wiki.php.net/rfc/counting_non_countables

Thanks,
Craig

---

## #96213 — Leigh — 2016-10-04T10:17:36+00:00

URL: https://externals.io/message/96211#96213 · In reply to: #96211

> Hi everybody
>
> I'd like to propose the introduction of warning when counting objects that
> can't be counted.
>
> The default behaviour is to return 1 for these objects, which can be
> misleading and hide bugs when attempting to count iterable objects (eg
> Generators). Adding a warning would alert developers to the issue
>
> https://wiki.php.net/rfc/counting_non_countables
>
> Thanks,
> Craig

I like this. I would personally like to see this behaviour deprecated
and removed in 8.0

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.

---

## #96214 — Craig Duncan — 2016-10-04T10:29:58+00:00

URL: https://externals.io/message/96211#96214 · In reply to: #96213

> 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) {

---

## #96232 — Markus Fischer — 2016-10-04T16:57:22+00:00

URL: https://externals.io/message/96211#96232 · In reply to: #96211

Hi,


> I'd like to propose the introduction of warning when counting objects that
> can't be counted.
> 
> The default behaviour is to return 1 for these objects, which can be
> misleading and hide bugs when attempting to count iterable objects (eg
> Generators). Adding a warning would alert developers to the issue
> 
> https://wiki.php.net/rfc/counting_non_countables

FTR, this also applies to certain non-object types too, see
https://3v4l.org/jqntq :

<?php

var_dump(count("string"));
var_dump(count(true));
var_dump(count(false));
var_dump(count(4321));

int(1)
int(1)
int(1)
int(1)

I was hit by this bug just last week :-(

- Markus

---

## #96234 — Nikita Popov — 2016-10-04T17:10:49+00:00

URL: https://externals.io/message/96211#96234 · In reply to: #96214

> 
>
> > […]
>
> 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

---

## #96235 — Craig Duncan — 2016-10-04T17:26:26+00:00

URL: https://externals.io/message/96211#96235 · In reply to: #96234

>
> 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.
>
>
Ok, but if we're saying that count("string") === 1 is no longer a feature
then presumably it makes sense to deprecate in 7.2 and remove in 8.0?

Just raising a warning but still providing the feature feels weird to me

---

## #96313 — Craig Duncan — 2016-10-11T09:49:43+00:00

URL: https://externals.io/message/96211#96313 · In reply to: #96211

I've updated the RFC now to take the deprecation route, and included
counting scalars:

https://wiki.php.net/rfc/counting_non_countables

The only remaining issue is what to do about handling *count(null)*
I think this should be deprecated too, but as it's the only case that
returns zero I wasn't sure whether other folk felt differently?

---

## #96317 — Leigh — 2016-10-11T13:36:24+00:00

URL: https://externals.io/message/96211#96317 · In reply to: #96313

> I've updated the RFC now to take the deprecation route, and included
> counting scalars:
>
> https://wiki.php.net/rfc/counting_non_countables
>
> The only remaining issue is what to do about handling *count(null)*
> I think this should be deprecated too, but as it's the only case that
> returns zero I wasn't sure whether other folk felt differently?

I'd agree that count(null) has no real benefit, and is probably an error.

---

## #96398 — Craig Duncan — 2016-10-17T20:50:34+00:00

URL: https://externals.io/message/96211#96398 · In reply to: #96211

I've updated the RFC now to include count(null) which resolves the final
open question.

If there isn't any more feedback I'll open voting in a few days

https://wiki.php.net/rfc/counting_non_countables

Thanks,
Craig

---

## #96399 — Nikita Popov — 2016-10-17T20:57:13+00:00

URL: https://externals.io/message/96211#96399 · In reply to: #96398

> I've updated the RFC now to include count(null) which resolves the final
> open question.
>
> If there isn't any more feedback I'll open voting in a few days
>
> https://wiki.php.net/rfc/counting_non_countables
>
> Thanks,
> Craig
>

I'm not sure I understand the motivation for throwing a deprecation notice
instead of a warning. In particular, what is the action that will be taken
here in the next major version? I guess we would throw a warning and return
false (instead of 0/1). But is the change of return value from 0/1 to false
really sufficiently worthwhile to go with a deprecation first?

In any case, if you want to go with deprecation, please specify what action
this RFC implies for PHP 8.

Nikita

---

## #96400 — Craig Duncan — 2016-10-17T21:09:42+00:00

URL: https://externals.io/message/96211#96400 · In reply to: #96399

>
>
> I'm not sure I understand the motivation for throwing a deprecation notice
> instead of a warning. In particular, what is the action that will be taken
> here in the next major version? I guess we would throw a warning and return
> false (instead of 0/1). But is the change of return value from 0/1 to false
> really sufficiently worthwhile to go with a deprecation first?
>
>
I don't feel strongly either way, as long as there's some clue that
something's not right.

Is there precedent for adding warnings in a minor? Would there be BC
concerns there?
I wouldn't want an RFC for a warning to fail when people would have voted
for a deprecation.



> In any case, if you want to go with deprecation, please specify what
> action this RFC implies for PHP 8.
>
>
Would it be acceptable for the RFC to state that this has no implications
for PHP 8, and is an indefinite deprecation?

---

## #96428 — Christoph M. Becker — 2016-10-18T11:53:20+00:00

URL: https://externals.io/message/96211#96428 · In reply to: #96400

> 
> > […]
>
> I don't feel strongly either way, as long as there's some clue that
> something's not right.
> 
> Is there precedent for adding warnings in a minor? Would there be BC
> concerns there?

<https://wiki.php.net/rfc/notice-for-non-valid-array-container> is
supposed to introduce a new `E_WARNING` in PHP 7.2.

And yes, there is a (small) BC break, but even `E_DEPRECATED` might be
regarded as such.

> I wouldn't want an RFC for a warning to fail when people would have voted
> for a deprecation.

You might consider offering this as voting option.

> > […]
>
> Would it be acceptable for the RFC to state that this has no implications
> for PHP 8, and is an indefinite deprecation?

In my opinion, an indefinite deprecation doesn't make much sense.

-- 
Christoph M. Becker

---

## #96492 — Craig Duncan — 2016-10-19T14:38:17+00:00

URL: https://externals.io/message/96211#96492 · In reply to: #96428

> > […]
> notice
> > […]
> taken
> > […]
>


<https://wiki.php.net/rfc/notice-for-non-valid-array-container> is

> supposed to introduce a new `E_WARNING` in PHP 7.2.
>
> And yes, there is a (small) BC break, but even `E_DEPRECATED` might be
> regarded as such.
>


Thank you both for the feedback, I've gone with a warning now, I'll put the
RFC to vote in a few days

https://wiki.php.net/rfc/counting_non_countables
