Re: Invokation on __toString() for object used as array key
| From: | Leigh | Date: | Thu, 25 Sep 2014 09:26:16 +0000 |
| Subject: | Re: Invokation on __toString() for object used as array key | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-77617@lists.php.net to get a copy of this message | ||
On 25 September 2014 09:21, Lester Caine <lester@lsces.co.uk> wrote:
>
> 'This function returns a unique identifier for the object.'
> There is a mistaken view in some usage that that will be DIFFERENT for
> different versions of the same object.
Not sure who has that view. It's quite clearly documented, for the
lifetime of an object, it will always have the same hash.
> So what *IS* returned by spl_object_hash?
> The UCN from 7 years ago flags that the identifier is only based on the
> internal pointers to the object in memory and that the result is not
> user friendly, but without digging into the code, the problems such as
> the fact that re-use of the buffer space by a later object cached in the
> same buffer may be the same as ID?
Yes it is created using various internal pointers, and is about as
user-friendly as any other kind of hash.
It is possible to create a duplicate hash if you unset an object,
force GC collection, and then create a new object which can occupy the
same memory location.
var_dump(spl_object_hash(new stdClass()));
gc_collect_cycles();
var_dump(spl_object_hash(new stdClass()));
This is a fairly unrealistic scenario, however Joe Watkins has
developed a better hash method that doesn't have this flaw, I don't
know if he ever intended to have it applied to core or not.