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

From: Date: Mon, 11 Jul 2016 20:48:41 +0000
Subject: Req #52807 [Com]: magic __toArray() for objects
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202214@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: geiger at karim dot email 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: 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. Previous Comments: ------------------------------------------------------------------------ [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']] ------------------------------------------------------------------------ [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? ------------------------------------------------------------------------ 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

« previous php.bugs (#202214) next »