Req #54082 [Opn->Wfx]: Cast array key objects to string

From: Date: Thu, 15 Jul 2021 14:05:59 +0000
Subject: Req #54082 [Opn->Wfx]: Cast array key objects to string
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235059@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=54082&edit=1 ID: 54082 Updated by: nikic@php.net Reported by: slusarz at curecanti dot org Summary: Cast array key objects to string -Status: Open +Status: Wont fix Type: Feature/Change Request Package: Arrays related Operating System: Irrelevant PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2017-01-11 14:39:44] 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); ------------------------------------------------------------------------ [2015-12-20 15:45:15] reallfqq-php at yahoo dot fr Still true in PHP 7.0.0... Will you fix it? ------------------------------------------------------------------------ [2014-05-25 01:44:16] 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... ------------------------------------------------------------------------ [2011-10-14 22:02:07] maschingan at gmail dot com The same issue. ------------------------------------------------------------------------ [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

« previous php.bugs (#235059) next »