PHP RFC: Making stdClass iterable
By far the most common usage of
stdClass is (as the manual defines) “Created by typecasting to object”, or from the result of a call to 'json_decode()' using the default parameters.
In both of these scenarios the resulting object is analogous to a plain array. Although it is a class, it has no methods or private/protected properties, it is simply an array that uses a different syntax for accessing its elements.
iterable type (introduced in PHP 7.1) does not accept this object, despite accepting the virtually identical array it was born from.
This RFC proposes that
stdClass is changed to implement the
Traversable interface so that code using the
iterable type can accept/return these objects.
Backward Incompatible Changes
This RFC aims to be fully backward compatible, although the implementation of the iteration will likely change, all existing behaviour of iterating over
stdClass will be retained.
Proposed PHP Version(s)
Proposed Voting Choices
Simple Yes/No vote that doesn't change the language itself so requires 50% + 1 votes to get 'accepted'.
An initial proof of concept is available here: https://github.com/php/php-src/pull/3388 (note that this implementation is not complete, it still has some edge cases and may change significantly)