Re: [RFC - discussion] __toArray()
| From: | Steven Wade | Date: | Tue, 04 Feb 2020 13:47:13 +0000 |
| Subject: | Re: [RFC - discussion] __toArray() | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108382@lists.php.net to get a copy of this message | ||
>> 3. The weak point of this proposal is the by reference handling for sort et al.
>> Counterpoint: if you pass a variable to preg_match, then matches gets converted from anything to
>> array, so i believe by reference casting should change the original value (https://3v4l.org/XUJ5m
>> <https://3v4l.org/XUJ5m>). This is weird, but consistent.
>
> The proposal states that “array functions that operate on an array by reference such as sort
> or shuffle will not work on an object implementing __toArray() under this proposal”, and IMO that
> is consistent with other magical casting behaviors and I wouldn’t expect a class implementing
> __toArray() to be able to be written or referenced like a traditional array.
>
> Passing an object with toString by reference will change the original variable:
> https://3v4l.org/77lov
> <https://3v4l.org/77lov>
Ah, I think I see what you mean. PHP’s making a copy of the string variable once it’s cast, so
it’s no longer referencing the original object that implemented the __toString() method. I
imagined the __toArray() would function the same. Once the cast is called on the object, it’s a
new array variable and can be acted upon as such. What I was trying to get at was that the magic
method does not give the ability to write to the original object. If that’s not clear then I need
to update the RFC.