Re: Invokation on __toString() for object used as array key

From: Date: Wed, 24 Sep 2014 12:24:35 +0000
Subject: Re: Invokation on __toString() for object used as array key
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-77577@lists.php.net to get a copy of this message
Hi, On Tue, 2014-09-23 at 10:04 +0200, Nicolai Scheer wrote: > until 5 minutes ago I thought it would be perfectly legal to use an object > as an array key, given that its __toString() method is in place. 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. johannes

« previous php.internals (#77577) next »