Req #52807 [Com]: magic __toArray() for objects

From: Date: Tue, 29 Oct 2019 12:46:49 +0000
Subject: Req #52807 [Com]: magic __toArray() for objects
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-223502@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=52807&edit=1

 ID:                 52807
 Comment by:         php at shark dot kom dot cz
 Reported by:        jtegwen at gmail dot com
 Summary:            magic __toArray() for objects
 Status:             Open
 Type:               Feature/Change Request
 Package:            Class/Object related
 Operating System:   n/a
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[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

------------------------------------------------------------------------
[2015-01-15 09:18:53] bpolaszek at gmail dot com

I really think this is relevant.
PHP 5.4 introduced something similar with the JsonSerializable interface.
When you call json_encode on an object that implements it, it automatically calls the
jsonSerialize() method that returns a string, an array, an integer, a float or a boolean.

When you implement the jsonSerialize() method, you generally want to have a json-friendly
representation of your current object, and you normally return an associative array with the
properties you want to output. That looks like a __toArray() method, doesn't it ?

What would be very nice is the following code to work :
class A {

    protected   $property1 = 'foo', 
                $property2 = ['foo', 'bar'], 
                $property3;

    public function __toArray() {
        return [
            'property1' =>  $this->property1,
            'property2' =>  $this->property2,
        ];
    }

}

class B {
    public static function doSomethingWithData(array $data) {
        // ...
    }
}

$a  =   new A;
B::doSomethingWithData((array) $a);
array_change_key_case($a, CASE_UPPER); // Would automatically cast $a to array and output
['PROPERTY1' => 'foo, 'PROPERTY2' => ['foo',
'bar']]

------------------------------------------------------------------------


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


Thread (13 messages)

« previous php.bugs (#223502) next »