# RFC: count_elements handler vs count()

- Status: Implemented (PHP 5.3)
- Authors: Etienne Kneuss colder@php.net
- Date: 2008-06-25
- Version: 1.0
- Original: https://wiki.php.net/rfc/array_count_handlers
- Page: https://externals.io/rfc/array_count_handlers
- 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

## Discussions on the mailing list

- [\[RFC\] Modify resolution order of count_elements handler](https://externals.io/message/38701.md) — started on 2008-07-02, 2 messages

---

## Introduction

An internal class has two ways to achieve a special feature:

1. implement the interface and define the method
2. define the handler

for a class to be userland-friendly AND fast it must:

1. define a zend\_function pointer in the object for a specific method
2. when creating the object, check if the method is overriden, if so, assign that pointer to the child method
3. define a handler that will check that pointer, if defined: call it, if not, proceed without any method call

## Proposal and Patch

There are one problem with how the count\_elements handler is defined/used: if an internal class extends Countable, it will call its count() method regardless of the handler pointer.

There are two main reasons why it's a problem:

1. while it's userland friendly, it doesn't give the possibility of internals class to spare a method call if the method is not overriden.
2. it's not consistent with other handlers

The patch consists of changing the order of the checks to fix that problem:

<http://patches.colder.ch/php-src/array-1.308.2.21.2.37.2.34.patch?markup><http://patches.colder.ch/php-src/array-1.454.patch?markup>

## Possible Breaks

Internal classes that define a count\_elements handler will have to be fixed.

## Changelog
