Req #54082 [NEW]: Cast array key objects to string
| From: | slusarz at curecanti dot org | Date: | Wed, 23 Feb 2011 18:47:47 +0000 |
| Subject: | Req #54082 [NEW]: Cast array key objects to string | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-158984@lists.php.net to get a copy of this message | ||
From:
Operating system: Irrelevant
PHP version: Irrelevant
Package: Arrays related
Bug Type: Feature/Change Request
Bug description:Cast array key objects to string
Description:
------------
Per http://www.php.net/manual/en/language.types.array.php,
array keys can
be integers or strings.
If an object can be converted to a string representation (i.e. it has a
__toString() method defined), it should be automatically cast to a string.
Currently, an error is thrown instead.
Current workaround is to manually cast to string when setting a key.
Test script:
---------------
class Foo {
public function __toString() {
return 'A';
}
}
$a = new Foo();
$b = array($a => 1);
print_r($b);
Expected result:
----------------
Array
(
'A' => 1
)
Actual result:
--------------
PHP Warning: Illegal offset type [...]
Array
(
)
--
Edit bug report at http://bugs.php.net/bug.php?id=54082&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=54082&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=54082&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=54082&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=54082&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=54082&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=54082&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=54082&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=54082&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=54082&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=54082&r=support
Expected behavior: http://bugs.php.net/fix.php?id=54082&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=54082&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=54082&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=54082&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=54082&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=54082&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=54082&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=54082&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=54082&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=54082&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=54082&r=mysqlcfg