Re: [PHP-DEV] [RFC - discussion] __toArray()

This is only part of a thread. view whole thread
February 4, 2020 14:13 (Benjamin Eberlei)
On Tue, Feb 4, 2020 at 2:43 PM Marco Pivetta <> wrote:

> > > On Tue, Feb 4, 2020, 14:36 Benjamin Eberlei <> wrote: > >> >> >> On Tue, Feb 4, 2020 at 2:10 PM Marco Pivetta <> wrote: >> >>> Linking (again) previous discussions: >>> >>> >>> `__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 ( >>>, >>> >>> >> >> 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. >
I believe the definition of BC break is "can an existing code-base run on the new PHP version without changes" and here the answer is yes, since __toArray fn's are not used by any existing code bases. What you refer to is, can existing code work with new code using this feature, and then the answer is indeed no. But the same was true for example for typed properties, which also requires additional handling in most meta programming libraries, and many other new features that existing libraries needed to adapt to: namespaces, scalar type hints, return types, ....