Re: [RFC] Using objects as keys

From: Date: Mon, 27 Oct 2014 17:02:43 +0000
Subject: Re: [RFC] Using objects as keys
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-78394@lists.php.net to get a copy of this message
On Mon, 2014-10-27 at 09:35 +0100, Michael Wallner wrote: > Actually, I see spl_object_hash($this) the 90% implementation of > __hash(), so how about making it the default for any object? I agree - it might be a good default. To Will's question: It is not sufficient for all cases. Having a custom implementation allows the reverse. $u = new ustring("....."); $a[$u] = 42; unset($u); foreach ($a as $k => $v) { $u = u($k); // u() is a bad name, see other thread for that debate } also spl_object_hash is a unique identify, but given value objects having them in array_keys might mean that one doesn't want identity but grouped by value ... function get_date_for_item($item) { return new DateTime(...); } foreach ($items as $item) { $counts[get_date_for_item($item)]++; } here we create a new Datetime instance for each item, while multiple items might share the same data. in the loop we're counting distinct dates not DateTime instances. johannes

« previous php.internals (#78394) next »