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

This is only part of a thread. view whole thread
  108377
February 4, 2020 13:39 stevenwadejr@gmail.com (Steven Wade)
> 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.
  108380
February 4, 2020 13:43 kontakt@beberlei.de (Benjamin Eberlei)
On Tue, Feb 4, 2020 at 2:39 PM Steven Wade <stevenwadejr@gmail.com> wrote:

> > 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. >
Yes, i see that it auto casts when you typehint for "array". print_r argument is not array but mixed, so it should not cast the object to array there.
> > 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). 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
  108382
February 4, 2020 13:47 stevenwadejr@gmail.com (Steven Wade)
>> 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.