Re: Invokation on __toString() for object used as array key
| From: | Andrea Faulds | Date: | Wed, 24 Sep 2014 19:45:31 +0000 |
| Subject: | Re: Invokation on __toString() for object used as array key | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-77588@lists.php.net to get a copy of this message | ||
On 24 Sep 2014, at 13:24, Johannes Schlüter <johannes@schlueters.de> wrote:
> Taking this sample code:::
>
> <?php
> class C {
> function __toString() {
> return "C";
> }
> }
>
> $a = [];
>
> $c1 = new C();
> $c2 = new C();
>
> $a[$c1] = 23;
> $a[$c2] = 42;
> ?>
>
> There the assumption would be that this leads to an array $a with two
> elements, where in fact there is only one if __toString() is being
> called. The only thing "making sense" would be using using the objects
> identity (i.e. via spl_object_hash()) everything else leads to issues.
I’m not sure this is a fair example. Don’t classes usually implement a
__toString()
that is based on the data they contain? If they don’t, I’m not sure they’re useful for
indexing anyway.
--
Andrea Faulds
http://ajf.me/