Re: [PHP-DEV] Proposal for a RFC

This is only part of a thread. view whole thread
  105622
May 7, 2019 12:58 ocramius@gmail.com (Marco Pivetta)
Hey Steven,


On Tue, 7 May 2019, 14:25 Steven Wade, <stevenwadejr@gmail.com> wrote:

> > I’m not against the proposal. I’m mainly asking if there are other ways > in the language today to accomplish similar things. I would prefer to see > interfaces used over more magic methods (something like ArraySerializable). > > A Twitter user pointed out that 7.4 is adding two new magic methods < > https://wiki.php.net/rfc/custom_object_serialization> - __serialize() and > __unserialize(). So adding more magic methods to PHP isn't unprecedented. > > On the same note, casting is already magical, so controling the > implementation via a magic method is more in line with current > functionality. >
With your current proposal so far, I'd simply have to throw an exception: `if (method_exists($object, '__toArray')) { throw UnsupportedObject::from($object); }` The alternative is to expand the reflection API with an array cast operation in there, and then migrate existing userland usages to that to retain BC. In addition to that, as someone that used to write a lot of `__toString()` (and now doesn't do that anymore), a clear API and interfaced (non-magic) `toArray()` is more powerful and useful. Greets, Marco
  105623
May 7, 2019 13:08 stevenwadejr@gmail.com (Steven Wade)
Hi Marco,

> The alternative is to expand the reflection API with an array cast operation in there, and then migrate existing userland usages to that to retain BC.
That's an interesting proposal - adding new reflection abilities. Hm..
> In addition to that, as someone that used to write a lot of `__toString()` (and now doesn't do that anymore), a clear API and interfaced (non-magic) `toArray()` is more powerful and useful.
I see you and I hear you. After 12 years, still like the magic of PHP. I like that casting is quick, simple, and is less verbose when writing/reading. But I'm not opposed to further discussing possible solutions. If this gets to the RFC stage, I'd prefer it be the best option for the language and most likely to pass. -- Steven Wade stevenwadejr@gmail.com