Re: New function: spl_object_id() or spl_object_handle()

From: Date: Sat, 29 Nov 2014 18:22:04 +0000
Subject: Re: New function: spl_object_id() or spl_object_handle()
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-79313@lists.php.net to get a copy of this message
On 29/11/2014 18:04, Bostjan Skufca wrote:
On 29 November 2014 at 00:21, Rowan Collins <rowan.collins@gmail.com> wrote:
On 28/11/2014 01:13, Bostjan Skufca wrote:
A function called spl_object_hash() exists, but it returns identical hashes for equal objects.
In case it's been lost in the noise, no it doesn't.
Ouch. I am terribly sorry about this mistake, it really does not. However, having word "hash" in the function name suggests that returned hash is generated by the object's contents, too. I just noticed that the first comment explains that this is not the case, which I confirmed with additional testing. To quote a part of first sentence of Wikipedia on "Hash function": "...with slight differences in input data producing very big differences in output data." So, to sum it up: spl_object_hash indeed does what I initially required, but I was mislead by the "hash" word in its name, and the environment where I was trying to use it (unable to bring two object references in the same place easily). But I would maybe suggest changing the name to spl_object_id (and keeping ..._hash alias for backward compatibility).
I can see the confusion, but the output is more like a hash than an ID - it's just that the input of the hashing is the object's immutable metadata, not its current internal state. Specifically, it's an arbitrary value which has no meaning beyond being unique for each input, and easily comparable between outputs. That said, it does do rather poorly on the "small difference in input, large difference in output" stakes; it could do with being run through something like MD5 to make it more well distributed, and easier for debugging by human eyes. Of course, you can always just do that in userland with md5(spl_object_hash($foo)) Regards, -- Rowan Collins [IMSoP]

« previous php.internals (#79313) next »