Skip to content

ArrayAccess and foreach #4790

@staabm

Description

@staabm

Description

the docs describe foreach as

The foreach construct provides an easy way to iterate over arrays and Traversable objects. foreach will issue an error when used with a variable containing a different data type or with an uninitialized variable.

while working on a PHPStan issue, I recently discovered that php also allows to iterate over an object implementing ArrayAccess, even though thats neither Traversable nor is_iterable nor is_array.

see https://3v4l.org/NMrFZ#veol


I am a bit lost whether

  • this actually works like expected?
  • this needs a doc update?
  • php-src misses to issue a error/warning?
  • whether I am overlooking something

PHP Version

any since PHP 7.1+

Operating System

macOS sequioa

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions