Edit report at https://bugs.php.net/bug.php?id=54082&edit=1

 ID:                 54082
 Comment by:         maschingan at gmail dot com
 Reported by:        slusarz at curecanti dot org
 Summary:            Cast array key objects to string
 Status:             Open
 Type:               Feature/Change Request
 Package:            Arrays related
 Operating System:   Irrelevant
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

The same issue.


Previous Comments:
------------------------------------------------------------------------
[2011-02-23 19:47:46] slusarz at curecanti dot org

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 this bug report at https://bugs.php.net/bug.php?id=54082&edit=1

Reply via email to