Re: Proposal for a RFC
| From: | Steven Wade | Date: | Thu, 13 Jun 2019 14:23:06 +0000 |
| Subject: | Re: Proposal for a RFC | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-105906@lists.php.net to get a copy of this message | ||
Apologies for the super late response:
> A recursive method would certainly have more value, because it actually
> does something more than translate one syntax to another. On the other
> hand, the use case that comes to mind is serialization, and we already have
> more specific methods and systems for that.
How could this new magic method be recursive? If it only works if you manually declare __toArray()
in your class, wouldn't you then as the user be in charge of casting anything manually in your
method implementation?
>
> I guess that's what it comes down to, what *specific* use cases would this
> feature be intended to help with? Is there some code of your own that
> inspired you to propose it, or something you've seen publically that would
> benefit from it?
Originally, it was inspired by seeing Laravel's use of Arrayable as an interface and if
something implements that, calling that class'
toArray() method, and wishing that
was built in so that frameworks didn't re-invent the wheel every time.
As far as in my code, collections being cast as an array easily would be nice. Models with
relationships, being able to implement that cast and control how your model and its children are (or
aren't) represented. That's useful for returning an array in a controller for an API, or
for simply adding context to a log message.
IMO, the point is, it's another tool in the developers arsenal that they can use when they see
fit. Not everyone will use it and not everyone will see the benefit of it, and that's ok, but
for those that would and could, __toArray() is for them (and me).
--
Steven Wade
stevenwadejr@gmail.com