On Sat, Jan 12, 2019 at 8:39 PM Craig Duncan <p...@duncanc.co.uk> wrote:

> Hi everybody
>
> I'd like to propose a change to stdClass so that it can satisfy the
> iterable parameter/return type.
>
> Firstly I acknowledge that this isn't an ideal solution, but I think it
> gets us to a better state than we're in today. It shouldn't be necessary,
> but while we have such a common function (json_decode) returning stdClass
> we should make it easy/unsurprising to work with.
>
> https://wiki.php.net/rfc/iterable-stdclass
>
> Thanks,

Craig
>

Rather than implementing Traversable, I think it would be much better to
implement IteratorAggregate and provide a proper getIterator()
implementation. Objects that only implement Traversable are a PITA to work
with and lots of code assumes that anything Traversable will either be
Iterator or IteratorAggregate. For userland code this is strictly enforced,
but internal code can avoid this. However, the current exceptions to this
are rather niche, while stdClass is somewhat commonly used.

I'm in favor of the general change here. What I mainly have in mind is that
we should stop allowing iteration over arbitrary objects in the future (we
did the first step by excluding them from "iterable") and instead require
the use of an explicit PropertyIterator, or similar. However, we'd probably
still want to allow direct iteration of stdClass and the cleanest way to do
that is to make it Traversable (via IteratorAggregate).

Nikita

Reply via email to