Req #52657 [Opn->Wfx]: Object id function where its uniqueness persists through entire script execution

From: Date: Wed, 10 Jun 2020 10:40:01 +0000
Subject: Req #52657 [Opn->Wfx]: Object id function where its uniqueness persists through entire script execution
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-227401@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=52657&edit=1

 ID:                 52657
 Updated by:         nikic@php.net
 Reported by:        marco dot weber at uni-trier dot de
 Summary:            Object id function where its uniqueness persists
                     through entire script execution
-Status:             Open
+Status:             Wont fix
 Type:               Feature/Change Request
 Package:            SPL related
 Operating System:   ANY
 PHP Version:        PHP >= 5.2.0
 Block user comment: N
 Private report:     N

 New Comment:

Marking this won't fix per my previous comment. If you use the object ID, you need to store the
object as well.

You may also want to consider using WeakReference (PHP 7.4) or WeakMap (PHP 8.0), for the case where
you are not interested in forcing the object to stay alive, but need to be notified if the object ID
has been released for re-use.


Previous Comments:
------------------------------------------------------------------------
[2018-10-05 17:56:56] nikic@php.net

There are many people here saying that such a function is necessary, but the use-case is not
entirely clear to me. The usage shown by OP is actually perfectly fine and exactly how
spl_object_hash() and spl_object_id() are supposed to be used: If you use the hash, you should also
store the object. That ensures that the hash will not reused while the hash is in use.

My suspicion is that in most cases either a) you would actually be fine with spl_object_hash() but
you may not realize it, or b) what you are really looking for are weak references or weak maps,
rather than object hashes.

------------------------------------------------------------------------
[2015-04-19 15:55:35] rmf@php.net

As noted in the original title, a name of spl_object_id was suggested. I would like to propose
something more suggestive of the intent: spl_object_uuid.

While I went ahead and assigned this to myself because I would like to find a solution for my own
needs, this is not high priority, and I am not a regular PHP contributor, so the following two
things must fall into line for this to become a reality:

1. I can manage the time to investigate and implement an elegant and proper solution.
2. My patch is accepted.

I can absolutely guarantee this will not be in PHP 7. Based on my current schedule it'll likely
be a few minor releases after.

If anyone else have more ample time and wants to tackle this, feel free to snatch it away and assign
it to yourself ;-).

------------------------------------------------------------------------
[2014-10-08 12:47:30] adrien dot crivelli at gmail dot com

The workaround suggested by "maciej dot sz at gmail dot com" does not work, as it has the
exact same pitfall as the existing spl_object_hash() function.

I am in favor of modifying existing spl_object_hash(). Even if it is to return an int, instead of
string. I am pretty sure anybody using the function would actually be happier if we could guarantee
uniqueness for the entire PHP process lifetime.

------------------------------------------------------------------------
[2013-09-19 22:41:38] metamarkers at gmail dot com

Here's a chopped down version of my current solution.

function id ( $object ) {
    static $id = 0;
    if (!isset($object->__id) || $object->__id[0] !== spl_object_hash($object)) 
{
        $object->__id = [spl_object_hash($object),++$id];
    }
    return $object->__id[1];
}

It works for clones to a degree. Clones can be made through this function to 
automatically refresh the id.

function dupe ( $object ) {
    $clone = clone $object;
    id($clone);
    return $clone;
}

------------------------------------------------------------------------
[2013-09-19 22:29:56] metamarkers at gmail dot com

Could be made into a magic property ->__id

The id could just be a signed 64-bit counter that ticks up every time an object 
is created. What would happen when you create more than 9,223,372,036,854,775,807 
objects? Refactor, probably.

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=52657


--
Edit this bug report at https://bugs.php.net/bug.php?id=52657&edit=1


Thread (15 messages)

« previous php.bugs (#227401) next »