Re: [PHP-DEV] Proposal for a RFC

This is only part of a thread. view whole thread
  105906
June 13, 2019 14:23 stevenwadejr@gmail.com (Steven Wade)
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
  105909
June 13, 2019 14:35 ocramius@gmail.com (Marco Pivetta)
On Thu, Jun 13, 2019 at 4:23 PM Steven Wade <stevenwadejr@gmail.com> wrote:

> > 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. >
Interestingly, my work day today is spent mostly removing this kind of behavior from a codebase riddled by it, replacing it with explicit conversions where needed. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/