> I am open to the idea of having __toArray. I just have a few questions about 
> the RFC details.
> 
> 1. print_r($object) would somehow call __toArray you say. Why would it cause 
> a cast when nothing else is cast? I would prefer print_r((array) $person);

Originally I intended the proposal to be specific to user casting like you 
suggested but when writing the RFC I decided to pursue making the magic method 
and casting more in line with the behavior of __toString(). For example, when 
an object implements the __toString() method and you simply “echo $obj” PHP 
automatically casts the object for you.

> 2. In the parameter and return type examples you should add 
> declare(strict_types=0); so it's clear this only works in weak mode.

I think this is a good idea. I hadn’t looked into strict_types and how it 
handles strings and their casting. Thanks!

> 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.

Reply via email to