On Mon, Jan 14, 2019 at 3:48 PM Rowan Collins <rowan.coll...@gmail.com> wrote:
> On Mon, 14 Jan 2019 at 11:57, Nikita Popov <nikita....@gmail.com> wrote: > >> Rather than implementing Traversable, I think it would be much better to >> implement IteratorAggregate and provide a proper getIterator() >> implementation. > > > I think adding behaviour to stdClass is a precedent we should approach > with extreme caution. > > Currently, it has no methods, and matches no type checks other than itself > and "object"; in a sense, it doesn't even have any properties, just the > ability to add them dynamically. To me, that makes sense: it's the > "classless object", the absolute bare minimum any object could be. > > > >> 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. > > > I agree that this should be explicit, and it could probably be implemented > as a very light wrapper. > > > >> However, we'd probably still want to allow direct iteration of stdClass >> > > Why? What do you consider the role of stdClass to be, that means having it > opt in to this behaviour would always be appropriate? > The way I see it, stdClass is basically an array in object notation (and object passing semantics). It carries data in the form of key value pairs and has no associated logic. The only things you can do with it are access keys (properties) and ... iterate over them. Mind you, stdClass is an anti-pattern. Instead of stdClass you *should* just be using an array. We should never have added stdClass. But now that we do have it, and with the way it is typically being used, I think it's fully appropriate to iterate over it. That is much unlike most other (non-Traversable) objects, where iterating over them is only reasonable in a few rather specific scenarios (say, a serializer, cloner, hydrator or whatever -- something that explicitly is interested in inspecting properties of objects as a matter of representation). Nikita > If you want something iterable, you can convert your stdClass object to an > array, wrap it an iterator that accepts any object, or convert it however > you want into a "proper" object where you can define what behaviour you > want. > > Regards, > -- > Rowan Collins > [IMSoP] >