Req #72579 [Com]: Support (array)-casting for objects

From: Date: Tue, 12 Jul 2016 06:23:07 +0000
Subject: Req #72579 [Com]: Support (array)-casting for objects
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202226@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72579&edit=1 ID: 72579 Comment by: geiger at karim dot email Reported by: geiger at karim dot email Summary: Support (array)-casting for objects Status: Feedback Type: Feature/Change Request Package: Class/Object related PHP Version: Next Major Version Block user comment: N Private report: N New Comment: I see preserving keys could be a problem and there should be a way to create an array out of the values of an object only, like iterator_to_array($foo, false) does, but nevertheless, I think that "losing" duplicate keys can be seen as expected behaviour when casting to an array, since there are by definition no duplicate keys in an array. Take this example: $a = (int)3.14; What's $a going to be? Obviously, it'll result in a flat 3, because an integer just does not support decimals, and that's okay, since it is by fact an integer. Arrays in PHP don't support duplicate keys, hence, you won't get them and they'll be "lost". What arrays in PHP do support, are non-numeric keys, which means, you will get them. In my opinion, you have to deal with things like that manually if you're trying to cast one type to another. Previous Comments: ------------------------------------------------------------------------ [2016-07-11 21:12:51] nikic@php.net iterator_to_array() has two modes: It can either preserve the keys of the iterator, or it can reindex it. It depends on the iterator which of these behaviors is appropriate. Especially with generator functions it's very common that preserving keys will yield a result of limited usefulness, as duplicate keys are overwritten. If (array) were to automatically invoke iterator_to_array() in one of these modes, you'd probably end up with the wrong behavior half the time. ------------------------------------------------------------------------ [2016-07-11 21:08:36] geiger at karim dot email Thanks for the quick response. I didn't know that function and it does pretty much what I intend to do, but the problem is: I have to call it manually. I'd love to see that function being invoked automatically when casting to an array, since that's the point of having a cast option after all. In my mind it's not consistent to be able to foreach through an object as expected but getting something completely different when casting it to an array. ------------------------------------------------------------------------ [2016-07-11 20:58:07] requinix@php.net How about iterator_to_array()? http://php.net/iterator-to-array ------------------------------------------------------------------------ [2016-07-11 20:48:41] geiger at karim dot email Related To: Bug #52807 ------------------------------------------------------------------------ [2016-07-11 20:39:56] geiger at karim dot email Description: ------------ Hi there, I recently stumbled upon the request #52583 (magic __toArray() for objects) while searching for a way to cast an object containing a list of items to an array for using it on functions like array_map(). There are many options to get to where I want to be, for example implement an interface having a toArray() method, but all of that is quite inconsistent, I think. I can see how having yet another magic __toArray()-method may not be a good thing, since it just raises more questions and problems, so I'd rather suggest something, which I think is the next logical step. As you all know, we are already able to implement interfaces like ArrayAccess, Countable, and Iterator. If we've done all that, we're almost where we want to be: Having an object act like an array. We now are able to use it in a foreach loop, assign, unset and alter values using the array-syntax ($foo[] = 'bar';), etc. The only thing that does not work is casting it to an array. If we do so, we'll always get an key-value-pair of its properties. I can see where that's useful, but if we already have all that methods on that object (offset*, next, key, valid, ...), why don't we use them? I'm suggesting the following behaviour: If a plain object with no interfaces gets casted to an array, maintaining the key-value-conversion is a great idea. But, if the object implements the (or a) Iterator-interface, why can't we "foreach" through that object and combine its keys and values in a true array, making it compatible to all (PHP)-functions and the array type? I hope this isn't a duplicate entry of some other feature request I didn't find. If not, I'd love to hear your opinion on this and if you think this makes sense. Best regards Karim Test script: --------------- Since this example implements the Iterator interface, it's a bit too long to paste here. You can visit the Gist at: https://gist.github.com/KarimGeiger/e3e4fbc4a1ec8c6564347b8a2a217334 Expected result: ---------------- array(2) { 'foo' => string(3) "bar" 'baz' => int(123) } Actual result: -------------- array(1) { '\0*\0items' => array(2) { 'foo' => string(3) "bar" 'baz' => int(123) } } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72579&edit=1

« previous php.bugs (#202226) next »