Re: Add __toArray() method to objects that would be called on cast to array

From: Date: Thu, 16 Mar 2017 09:16:24 +0000
Subject: Re: Add __toArray() method to objects that would be called on cast to array
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-98565@lists.php.net to get a copy of this message
Hi Benoît, Benoît Burnichon wrote:
Looking at the manual http://php.net/manual/en/language.types.array.php#language.types.array.casting , it seems ArrayObject class does not comply to array casting because integer public properties could also be retrieved. Some tests showed that regular class always have string keys even when a `$key = 0; $this->{$key} = 'avalue'; is called. In this case, var_export((array)$object);` returns array('0' => 'avalue') (Notice the quote around key 0 - https://3v4l.org/6QW70)
I'm not sure what specifically you see to be the problem, but I think it might be worth directing you to my RFC which relates to this: https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts -- Andrea Faulds https://ajf.me/

« previous php.internals (#98565) next »