Hi Duncan,

I quite strongly dislike the idea, as it sounds like this solution is
a hack for a particular use case, and that use case doesn't seem to be
particularly important to me.

I say it's not particularly important to me, as you could solve it right now by:

i) asking json_decode to return an array rather than an object.
ii) converting the result from json_decode into an object that
implements traversable.
iii) By accepted that PHP's type system isn't powerful enough to make
all programs trivially type-safe, and so to not use the iterable
parameter type here. This won't make your program any more or less
correct, it will just be slightly more annoying to debug.

Those seem to be better than special casing StdClass.

I say it's a hack as you're forcing the language to fit a particular
need with the available tools now, rather than thinking through what
best end-goal would be.

I think the correct long-term solution for this would be for us to
revisit the union type RFC (https://wiki.php.net/rfc/union_types) and
so be able to define a type like:

type foreachable = object|iterable;

The union types RFC would support the problem you're trying to solve,
as well as other use cases without introducing special cases.

cheers
Dan
Ack

On Sat, 12 Jan 2019 at 19:39, 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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to