Req #72579 [Fbk->NoF]: Support (array)-casting for objects
| From: | php-bugs at lists dot php dot net | Date: | Sun, 24 Jul 2016 04:22:28 +0000 |
| Subject: | Req #72579 [Fbk->NoF]: Support (array)-casting for objects | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-202555@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
Updated by: php-bugs@lists.php.net
Reported by: geiger at karim dot email
Summary: Support (array)-casting for objects
-Status: Feedback
+Status: No Feedback
Type: Feature/Change Request
Package: Class/Object related
PHP Version: Next Major Version
Private report: N
New Comment:
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
Previous Comments:
------------------------------------------------------------------------
[2016-07-12 06:23:04] geiger at karim dot email
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.
------------------------------------------------------------------------
[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
------------------------------------------------------------------------
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=72579
--
Edit this bug report at https://bugs.php.net/bug.php?id=72579&edit=1