Re: Invokation on __toString() for object used as array key
| From: | Andrey Andreev | Date: | Tue, 23 Sep 2014 09:34:26 +0000 |
| Subject: | Re: Invokation on __toString() for object used as array key | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-77519@lists.php.net to get a copy of this message | ||
On Tue, Sep 23, 2014 at 12:29 PM, Andrea Faulds <ajf@ajf.me> wrote:
>
>> On 23 Sep 2014, at 10:15, Leigh <leight@gmail.com> wrote:
>>
>>> On 23 September 2014 09:51, Michael Wallner <mike@php.net> wrote:
>>>
>>> Yes, it was removed intentionally (quite a long time ago), like using
>>> resources as array keys, to avoid hard-to-trace bugs for the user. At
>>> least that's the reasoning I can remember.
>>
>> He doesn't want to add the object as a key, he wants to invoke __toString().
>>
>> Is there really any harm in adding a IS_OBJECT case to
>> zend_whatever_add_array_element, and checking if it has a __toString?
It does work if you do an explicit cast to string:
class Foo {
public function __toString()
{
return 'Bar';
}
}
$array = array();
$object = new Foo();
$array[(string) $object] = 'this works';
Cheers,
Andrey.