php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54082 Cast array key objects to string
Submitted: 2011-02-23 19:47 UTC Modified: 2021-07-15 14:05 UTC
Votes:16
Avg. Score:4.2 ± 0.8
Reproduced:16 of 16 (100.0%)
Same Version:6 (37.5%)
Same OS:7 (43.8%)
From: slusarz at curecanti dot org Assigned:
Status: Wont fix Package: Arrays related
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: slusarz at curecanti dot org
New email:
PHP Version: OS:

 

 [2011-02-23 19:47 UTC] 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
(
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-14 22:02 UTC] maschingan at gmail dot com
The same issue.
 [2014-05-25 01:44 UTC] chx@php.net
This is an absolute pain when dealing with strings that might be returned from autoescaped Twig as Twig_Markup objects. I'd warmly appreciate a bugfix even in 5.4...
 [2015-12-20 15:45 UTC] reallfqq-php at yahoo dot fr
Still true in PHP 7.0.0...
Will you fix it?
 [2017-01-11 14:39 UTC] zyss at mail dot zp dot ua
This feature is much awaited, it has wide practical application.

For example in ORM system fields references could be objects:

class FieldReference {
    protected $name;
    
    public function __construct($name) {
        $this->name = $name;
    }
    
    public function __toString() {
        return $this->name;
    }
}

$ref = new FieldReference('field');
echo $ref, "\n"; // outputs: field, __toString() method is called


$select = [ // works fine but (string) clutters code
    (string) $ref => 'value'
];
var_dump($select); // outputs: array(1) {  ["field"]=> string(5) "value" }


$select = [ // looks fine but triggers: Warning: Illegal offset type
    (string) $ref => 'value'
];
var_dump($select);
 [2021-07-15 14:05 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2021-07-15 14:05 UTC] nikic@php.net
I'm going to decline this one for forward compatibility reasons. We may want to support actual object keys in the future, and performing an implicit string cast would prevent that.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Oct 09 12:01:27 2024 UTC