Req #72579 [NEW]: Support (array)-casting for objects
| From: | geiger at karim dot email | Date: | Mon, 11 Jul 2016 20:40:05 +0000 |
| Subject: | Req #72579 [NEW]: Support (array)-casting for objects | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-202213@lists.php.net to get a copy of this message | ||
From: geiger at karim dot email
Operating system:
PHP version: Next Major Version
Package: *General Issues
Bug Type: Feature/Change Request
Bug description:Support (array)-casting for objects
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 bug report at https://bugs.php.net/bug.php?id=72579&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72579&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72579&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72579&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=72579&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=72579&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=72579&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=72579&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=72579&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=72579&r=support
Expected behavior: https://bugs.php.net/fix.php?id=72579&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=72579&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=72579&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=72579&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72579&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=72579&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=72579&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=72579&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72579&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=72579&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=72579&r=mysqlcfg