Edit report at https://bugs.php.net/bug.php?id=52807&edit=1
ID: 52807
Updated by: cmb@php.net
Reported by: jtegwen at gmail dot com
Summary: magic __toArray() for objects
-Status: Open
+Status: Suspended
Type: Feature/Change Request
Package: Class/Object related
Operating System: n/a
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
There is already a respective RFC[1] (currently under discussion),
so I'm suspending this ticket for the time being.
[1] <https://wiki.php.net/rfc/to-array>
Previous Comments:
------------------------------------------------------------------------
[2019-10-29 12:46:49] php at shark dot kom dot cz
A lot of functions depends on pure arrays even if any collection will be good enough.
So I suggest that object implementing __toArray() magic method will be casted using this method.
------------------------------------------------------------------------
[2019-10-29 12:44:06] php at shark dot kom dot cz
Related To: Bug #72579
------------------------------------------------------------------------
[2017-04-05 14:45:34] rulatir at wp dot pl
Because e.g. array_chunk() won't accept an object even if it meticulously implements all the
gazillion iteration and array access interfaces.
------------------------------------------------------------------------
[2016-07-11 20:48:40] geiger at karim dot email
Hi there,
sorry for warming this up one more time, but I think __toArray() isn't the perfect solution to
the problem. Since PHP is already able to use an object as an array using the Iterator interface,
why don't we just extend this?
I made my thoughts a bit clearer in request #72579, if anyone is interested.
------------------------------------------------------------------------
[2016-06-16 20:27:00] igorsantos07 at gmail dot com
Something that should be pointed out in the issue is that (array)$object is one of the most common
object casts used out there. String casting are useful in templating, while array casting is useful
for API returns, for instance... And there's currently no way to instinctively do this call in
a protective way, as we can do with (string)$object.
Calling $object->toArray() is bulky and unreliable: if the developer knows the object will have
that method (so they can call it), he already knows it needs further processing, just like string
casts do.
Furthermore, implicit casts are always unreliable without a __toArray() magic method.
As an API developer for a couple of years, I've stumbled upon this API discrepancy between
magic __toString() and explicit toArray() implementations. That's quite a bad smell for
PHP's OO system.
It seems there's already an RFC with a patch to discuss that. How's this going?
https://wiki.php.net/rfc/object_cast_to_types
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=52807
--
Edit this bug report at https://bugs.php.net/bug.php?id=52807&edit=1