Re: [RFC - discussion] __toArray()

From: Date: Tue, 04 Feb 2020 13:43:14 +0000
Subject: Re: [RFC - discussion] __toArray()
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-108379@lists.php.net to get a copy of this message
On Tue, Feb 4, 2020, 14:36 Benjamin Eberlei <kontakt@beberlei.de> wrote: > > > On Tue, Feb 4, 2020 at 2:10 PM Marco Pivetta <ocramius@gmail.com> wrote: > >> Linking (again) previous discussions: >> https://externals.io/message/98539#98539 >> >> __toArray as a magic function call when >> (array) cast happen is a bad >> idea: it is a BC break, and it removes one of the very few interactions >> (with objects) that didn't cause any side-effects ( >> https://externals.io/message/98539#98545, >> https://externals.io/message/98539#98567) >> > > I think we can't classify it as BC break, because no existing code > implements __toArray at the moment, and hence it will not fail when this > feature is introduced and code gets upgraded to newer versions. > It is a BC break because it changes the semantic of (array) $object: the operation is no longer stable between two versions of the language. Code relying on (array) behaviour (stable) requires additional reflection wrappers to check if $object is **not** implementing __toArray, and then the operation can be safely used (or an exception is to be thrown). You can most certainly make a new operation, such as (toArray) $object: the cast operator is new and isn't changing any existing behaviour.

« previous php.internals (#108379) next »