ArrayAccess: Constant numeric string offsets become integers

php.internals

Rudi Theunissen

9 years ago
Bug: https://bugs.php.net/bug.php?id=63217 PR: https://github.com/php/php-src/pull/2607 Demo: https://3v4l.org/dagUP I've heard mixed responses to this bug and the approach to fix it (or whether we should at all). The two common opinions are that ArrayAccess should behave exactly like an array, and the other is that it's up to the implementer to handle the offsets. I would like to propose that the aim of ArrayAccess is not to emulate the *behaviour* of an array, but instead only use its access *syntax*. The way an array handles numeric string keys, floats, booleans etc is specific to how an array works, and should not be forced on objects that implement ArrayAccess. In the demo linked above, you'll notice that ArrayAccess already allows any key type, but becomes inconsistent when a constant numeric string is used. Notice that the HHVM result shows the expected behaviour. We should either go full array behaviour, or full array syntax only. The numeric string optimisation that causes this bug is broken and should either be fixed or removed entirely. An important note is that *this does not affect arrays or object properties at all* - only ArrayAccess.

Sara Golemon

9 years ago
On Fri, Jul 7, 2017 at 7:30 PM, Rudi Theunissen <rtheunissen@php.net> wrote:
> Bug: https://bugs.php.net/bug.php?id=63217 > PR: https://github.com/php/php-src/pull/2607 > Demo: https://3v4l.org/dagUP > > We should either go full array behaviour, or full array syntax only. The > numeric string optimisation that causes this bug is broken and should > either be fixed or removed entirely. >
Agreed. offsetGet() et al should either normalize to keyish values, or do no normalization at all. The current PHP behavior is a half-measure and incorrect by any measure. I like HHVM's behavior here as it gives more power to the script dev. -Sara