rfc:iterable-stdclass

PHP RFC: Making stdClass iterable

Introduction

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.

However the iterable type (introduced in PHP 7.1) does not accept this object, despite accepting the virtually identical array it was born from.

Proposal

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)

PHP 7.4

Proposed Voting Choices

Simple Yes/No vote that doesn't change the language itself so requires 50% + 1 votes to get 'accepted'.

Vote

Making stdClass iterable
Real name Yes No
ajf (ajf)  
andrey (andrey)  
ashnazg (ashnazg)  
bishop (bishop)  
bwoebi (bwoebi)  
carusogabriel (carusogabriel)  
danack (danack)  
didou (didou)  
diegopires (diegopires)  
duncan3dc (duncan3dc)  
emir (emir)  
galvao (galvao)  
girgias (girgias)  
hywan (hywan)  
jhdxr (jhdxr)  
kalle (kalle)  
kelunik (kelunik)  
kguest (kguest)  
krakjoe (krakjoe)  
levim (levim)  
lex (lex)  
nikic (nikic)  
ocramius (ocramius)  
peehaa (peehaa)  
petk (petk)  
pmmaga (pmmaga)  
pollita (pollita)  
ramsey (ramsey)  
salathe (salathe)  
sebastian (sebastian)  
stas (stas)  
trowski (trowski)  
Final result: 7 25
This poll has been closed.

Implementation

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)

References

rfc/iterable-stdclass.txt · Last modified: 2019/04/21 13:05 by duncan3dc