# How to detect classes with a count_elements handler in userland?

- Thread: https://externals.io/message/96875
- Original archive: https://news-web.php.net/php.internals/96875
- Started by Christoph M. Becker on 2016-11-13T17:34:48+00:00
- Messages: 10
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #96875 — Christoph M. Becker — 2016-11-13T17:34:48+00:00

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

Hi!

In the discussion on PR #2185[1] we've stumbled upon the issue of
internal classes implementing a count_elements handler (but not the
Countable interface).

Is it possible to detect this in userland?  If not, I would suggest that
we introduce something like `is_countable()`, so users can check whether
 `count()` may be safely called on a given value.

[1] <https://github.com/php/php-src/pull/2185>

-- 
Christoph M. Becker

---

## #96877 — Niklas Keller — 2016-11-13T20:24:43+00:00

URL: https://externals.io/message/96875#96877 · In reply to: #96875

2016-11-13 18:34 GMT+01:00 Christoph M. Becker <cmbecker69@gmx.de>:

> Hi!
>
> In the discussion on PR #2185[1] we've stumbled upon the issue of
> internal classes implementing a count_elements handler (but not the
> Countable interface).
>
> Is it possible to detect this in userland?  If not, I would suggest that
> we introduce something like `is_countable()`, so users can check whether
> `count()` may be safely called on a given value.
>
> [1] <https://github.com/php/php-src/pull/2185>
>

How about just making those classes implement the interface instead?

Regards, Niklas

---

## #96878 — Craig Duncan — 2016-11-13T21:10:33+00:00

URL: https://externals.io/message/96875#96878 · In reply to: #96877

> How about just making those classes implement the interface instead?

Christoph pointed out that there may be classes in extensions that use
count_elements.

Also it would make userland code simpler:
is_countable($thing)
vs
is_array($thing) || $thing implements \Countable

---

## #96885 — Christoph M. Becker — 2016-11-14T13:22:46+00:00

URL: https://externals.io/message/96875#96885 · In reply to: #96878

> > […]
> 
> Christoph pointed out that there may be classes in extensions that use
> count_elements.

Furthermore, letting those classes implement Countable could break BC.

> Also it would make userland code simpler:
> is_countable($thing)
> vs
> is_array($thing) || $thing implements \Countable

Good catch!

Note that Craig has added `is_countable()` to PR #2185 (which implements
the RFC).  In my opinion, that is okay, but if anybody has objections,
we probably need another RFC.

-- 
Christoph M. Becker

---

## #96936 — Nikita Popov — 2016-11-16T21:02:45+00:00

URL: https://externals.io/message/96875#96936 · In reply to: #96885

On Mon, Nov 14, 2016 at 2:22 PM, Christoph M. Becker <cmbecker69@gmx.de>
wrote:

> 
>
> > […]
>
> Furthermore, letting those classes implement Countable could break BC.
>

How would this break BC?

SXE even already has a `count()` method, and count_elements respects
overrides of that method. Clearly SXE is supposed to implement Countable,
it was simply forgotten.

> Also it would make userland code simpler:
> > […]
>
> Good catch!
>
> Note that Craig has added `is_countable()` to PR #2185 (which implements
> the RFC).  In my opinion, that is okay, but if anybody has objections,
> we probably need another RFC.
>

This should be done as a separate change IMHO.

Nikita

---

## #96942 — Joe Watkins — 2016-11-17T07:30:40+00:00

URL: https://externals.io/message/96875#96942 · In reply to: #96936

Morning,

Just to chime in ... can you split the PR into the RFC, and the new thing
please.

Just another question on how we could make objects that have count_elements
(which is in object handlers) implement an interface on the class entry
(which is detached from handlers) ?

I'm sure it's doable, but it doesn't look very straight forward, some kind
of hacking in instanceof maybe ... not sure ...

Cheers
Joe



> On Mon, Nov 14, 2016 at 2:22 PM, Christoph M. Becker <cmbecker69@gmx.de>
> wrote:
>
> > […]
>
> How would this break BC?
>
> SXE even already has a `count()` method, and count_elements respects
> overrides of that method. Clearly SXE is supposed to implement Countable,
> it was simply forgotten.
>
> > […]
>
> This should be done as a separate change IMHO.
>
> Nikita

---

## #96943 — Craig Duncan — 2016-11-17T09:14:22+00:00

URL: https://externals.io/message/96875#96943 · In reply to: #96942

>
> Just to chime in ... can you split the PR into the RFC, and the new thing
> please.
>

Of course, that's done now: https://github.com/php/php-src/pull/2206

---

## #96946 — Nikita Popov — 2016-11-17T11:49:04+00:00

URL: https://externals.io/message/96875#96946 · In reply to: #96942

> Morning,
>
> Just to chime in ... can you split the PR into the RFC, and the new thing
> please.
>
> Just another question on how we could make objects that have
> count_elements (which is in object handlers) implement an interface on the
> class entry (which is detached from handlers) ?
>

> I'm sure it's doable, but it doesn't look very straight forward, some kind
> of hacking in instanceof maybe ... not sure ...
>

I don't think this needs any magic. We simply implement the interface by
convention. Just like you are supposed to implement Traversable if you're
implementing get_iterator. Similarly you should implement Countable if
you're using count_elements.

Nikita

---

## #96947 — Christoph M. Becker — 2016-11-17T11:59:49+00:00

URL: https://externals.io/message/96875#96947 · In reply to: #96936

> On Mon, Nov 14, 2016 at 2:22 PM, Christoph M. Becker <cmbecker69@gmx.de>
> wrote:
> 
> > […]
> 
> How would this break BC?
> 
> SXE even already has a `count()` method, and count_elements respects
> overrides of that method. Clearly SXE is supposed to implement Countable,
> it was simply forgotten.

Thinking about it, we most likely don't have to be concerned with some
*theoretic* BC break (such as `!(instance of Countable)`).

Thanks!

-- 
Christoph M. Becker

---

## #96957 — Joe Watkins — 2016-11-17T15:25:22+00:00

URL: https://externals.io/message/96875#96957 · In reply to: #96946

Afternoon,

A change in convention makes sense, and sounds much nicer than hacking
anything to make it work.

I do prefer this approach to yet-another-function.

Is changing convention all we would do though, or would we raise a warning
when handler is present and interface is not ?

Cheers
Joe



> On Thu, Nov 17, 2016 at 8:30 AM, Joe Watkins <pthreads@pthreads.org>
> wrote:
>
> > […]
>
> > […]
>
> I don't think this needs any magic. We simply implement the interface by
> convention. Just like you are supposed to implement Traversable if you're
> implementing get_iterator. Similarly you should implement Countable if
> you're using count_elements.
>
> Nikita
