# [RFC] Modify resolution order of count_elements handler

- Thread: https://externals.io/message/38701
- Original archive: https://news-web.php.net/php.internals/38701
- Started by Etienne Kneuss on 2008-07-02T11:57:06+00:00
- Messages: 2
- RFC: count_elements handler vs count() (Implemented (PHP 5.3)) — https://externals.io/rfc/array_count_handlers.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

---

## #38701 — Etienne Kneuss — 2008-07-02T11:57:06+00:00

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

Hi,

I'd like to fix the resolution order of Countable::count() vs
count_elements handler.
Currently, things are done like this:

1) check if the object implements countable, if so, call it's method
2) if not, check if count_elements handler is defined, if so, use it.

This is a problematic order of resolution, and I'd like to inverse it.

Why? Because :

1) it is inconsistent with other handlers
2) it makes it impossible to be userland friendly while saving a
method call when it's possible.

Currently, there are 3 possibilities:

1) the internal class doesn't use count_elements handler
-> No problem with the change

2) the internal class uses only the handler
-> It's not userland friendly, the change won't help, so it needs to
be fixed anyway

3) both the handler and the method are used
-> the handler was never called directly, such classes will need a fix
after that resolution order change.

Userland classes are not affected.
I can and will take care of every problems occuring in classes in
php-src (2 or 3, mostly SPL).

Patches and details:
http://wiki.php.net/rfc/array_count_handlers


Regards

-- 
Etienne Kneuss
http://www.colder.ch

Men never do evil so completely and cheerfully as
when they do it from a religious conviction.
-- Pascal

---

## #38788 — Marcus Boerger — 2008-07-06T11:01:14+00:00

URL: https://externals.io/message/38701#38788 · In reply to: #38701

Hello Etienne,

Wednesday, July 2, 2008, 1:57:06 PM, you wrote:

> Hi,

> I'd like to fix the resolution order of Countable::count() vs
> count_elements handler.
> Currently, things are done like this:

> 1) check if the object implements countable, if so, call it's method
> 2) if not, check if count_elements handler is defined, if so, use it.

> This is a problematic order of resolution, and I'd like to inverse it.

> Why? Because :

> 1) it is inconsistent with other handlers
> 2) it makes it impossible to be userland friendly while saving a
> method call when it's possible.

Sounds good to me and while you're at it. You can get rid of HAVE_SPL
checks as SPL is always present now.

> Currently, there are 3 possibilities:

> 1) the internal class doesn't use count_elements handler
->> No problem with the change

> 2) the internal class uses only the handler
->> It's not userland friendly, the change won't help, so it needs to
> be fixed anyway

> 3) both the handler and the method are used
->> the handler was never called directly, such classes will need a fix
> after that resolution order change.

Sounds like the interface_gets_implemented handler of the interface should
check that the count_elements handler is not set or is already set to the
same value anyway and throw an exception/issue an error otherwise.

> Userland classes are not affected.
> I can and will take care of every problems occuring in classes in
> php-src (2 or 3, mostly SPL).

> Patches and details:
> http://wiki.php.net/rfc/array_count_handlers


> Regards

> -- 
> Etienne Kneuss
> http://www.colder.ch

> Men never do evil so completely and cheerfully as
> when they do it from a religious conviction.
> -- Pascal




Best regards,
 Marcus
