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

From: Date: Fri, 28 Nov 2014 23:20:40 +0000
Subject: Re: New function: spl_object_id() or spl_object_handle()
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-79302@lists.php.net to get a copy of this message
On 28/11/2014 21:06, Marc Bennewitz wrote:
But to have a unique ID for objects sound like a very good idea and would solve the object as array key issue. I'm sure it's not as simple as the function prototype explained in this thread previously.
Indeed. Fortunately, we already have one: it's called spl_object_hash() If the current limitation of handles being potentially reused (which would also affect this proposal) can be solved, then it can be done there without creating a new function, as far as I can see. For quick comparison, here's the key line of the proposed implementation: RETURN_LONG(Z_OBJ_HANDLE_P(obj)); And here's the key lines of the spl_object_hash() implementation [1]: SPL_G(hash_mask_handle) = (intptr_t)(php_mt_rand(TSRMLS_C) >> 1); SPL_G(hash_mask_handlers) = (intptr_t)(php_mt_rand(TSRMLS_C) >> 1); ... hash_handle = SPL_G(hash_mask_handle)^(intptr_t)Z_OBJ_HANDLE_P(obj); hash_handlers = SPL_G(hash_mask_handlers)^(intptr_t)Z_OBJ_HT_P(obj); return strpprintf(32, "%016lx%016lx", hash_handle, hash_handlers); So, the existing function varies based on the object's "handle" (the part the simple implementation would return unhashed), its "handlers", plus some per-thread randomness. [1] http://lxr.php.net/xref/PHP_TRUNK/ext/spl/php_spl.c#756 -- Rowan Collins [IMSoP]

« previous php.internals (#79302) next »