Re: Proposal for a RFC

From: Date: Sun, 05 May 2019 14:59:57 +0000
Subject: Re: Proposal for a RFC
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-105596@lists.php.net to get a copy of this message
> On May 4, 2019, at 09:58, Steven Wade <stevenwadejr@gmail.com> wrote: > > Hi Internals team! > > I have an idea for a feature that I'd love to see in the language one day and wanted to > run the idea by you all. > > The idea is to add a new magic method "__toArray()" that would allow a developer to > specifiy how a class is cast to an array. The idea is the same mentality of __toString(), but, for > arrays. > > I would personally love this feature and those I've run it by were also excited by the > idea. So I'm soliciting feedback in hopes that things go well and I can officially write the > RFC. As for implementation, Sara Golemon is awesome and while chatting a few months back, knocked > out a proof-of-concept implementation > <https://github.com/sgolemon/php-src/tree/experimental.toarray>. > There's still work to be done if this proposal gets to the RFC phase, but again, just gauging > interest here. > > I appreciate any feedback you all can provide. > > Thanks, > > - Steven Wade Using existing language functionality, would it satisfy the similar needs if you implement IteratorAggregate and return an ArrayIterator from the getIterator() method? Obviously, the (array) cast wouldn’t use this, but you could do $object->getIterator()->getArrayCopy(). 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). -Ben

« previous php.internals (#105596) next »