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

From: Date: Thu, 15 Jan 2015 09:18:54 +0000
Subject: Req #52807 [Com]: magic __toArray() for objects
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-189965@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:         bpolaszek at gmail dot com
 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:

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']]


Previous Comments:
------------------------------------------------------------------------
[2012-09-24 12:50:52] umpirsky at gmail dot com

@dmgx This is not the problem the bug is about. What if you have some library 
which casts variables to arrays, like Zend_XMLRPC for example. Iterator or 
ArrayAccess will not do the job, since you need control over casting your class to 
array.

------------------------------------------------------------------------
[2010-10-11 17:17:20] dmgx dot michael at gmail dot com

I've found that when I have an object I need to behave like an array in some contexts that it
is worth the time to implement Iterator and Array in the object, which allows the object to behave
like an array.  It takes more effort than implementing a theoretical magic __toArray() method, but
its worth it. 

Read here for information on doing this: http://php.net/manual/en/language.oop5.iterations.php

------------------------------------------------------------------------
[2010-10-03 18:36:00] + at ni-po dot com

@aharvey: Couldn't the patch in the RFC you mentioned be modified to support arrays, too? Or
would the implementation for arrays be completely different to the one for arrays?

------------------------------------------------------------------------
[2010-09-12 04:06:35] aharvey@php.net

You are quite correct about request #52583. My apologies. I
remember skimming it when it came in, but obviously not very well. :)

To be honest, I think this is a better candidate for a thread on the
Internals mailing list or an RFC than a feature request here. I also
think this is extremely unlikely to make it in in any case,
particularly without a patch.

Nevertheless, I'll reopen this for now.

------------------------------------------------------------------------
[2010-09-10 17:21:11] jtegwen at gmail dot com

I found that request when researching this one. I read that as casting *to* an object not casting
*from* an object. 

Am I missing something?

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


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 (#189965) next »