Hey Steven,

On Tue, 7 May 2019, 14:25 Steven Wade, <stevenwad...@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

Reply via email to