# RFC: Making stdClass iterable

- Status: Declined
- Authors: Craig Duncan php@duncanc.co.uk
- Date: 2019-01-12
- Version: 0.2
- Original: https://wiki.php.net/rfc/iterable-stdclass
- Page: https://externals.io/rfc/iterable-stdclass
- 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

## Votes

### Making stdClass iterable (closed)

Yes: 7 · No: 25

Voters: ajf (No), andrey (No), ashnazg (No), bishop (Yes), bwoebi (Yes), carusogabriel (No), danack (No), didou (No), diegopires (No), duncan3dc (Yes), emir (No), galvao (Yes), girgias (No), hywan (No), jhdxr (No), kalle (No), kelunik (No), kguest (No), krakjoe (No), levim (No), lex (No), nikic (Yes), ocramius (No), peehaa (No), petk (No), pmmaga (No), pollita (Yes), ramsey (No), salathe (No), sebastian (No), stas (No), trowski (Yes)

## Discussions on the mailing list

- [Making stdClass iterable](https://externals.io/message/103718.md) — started on 2019-01-12, 14 messages
- [\[VOTE\] Making stdClass iterable](https://externals.io/message/104139.md) — started on 2019-02-04, 8 messages

---

## Introduction

By far the most common usage of `stdClass` is (as the [manual](http://php.net/manual/en/reserved.classes.php) 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

**Vote: Making stdClass iterable** — Yes: 7 · No: 25 (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

Discussion thread: <https://externals.io/message/103718>

Voting thread: <https://externals.io/message/104139>
