Editors Note, February 14th 2022: This project is now ABANDONED and no longer supported or updated.

asherwunk/phabstractic now has a feature for classes for generating unique identities: Identity Trait

Identity Management

This is a very straightforward feature:

trait Identity
{
    
    protected static $identityCounter = 0;
    
    protected $identityPrefix = '';
    
    protected function getNewIdentity()
    {
        static::$identityCounter++;
        return $this->identityPrefix . static::$identityCounter;
    }
    
}

The feature defines a static property for the user class.  This static counter remains unique on each poll for a new identity (in this case it’s an integer).  The $identityCounter must be ‘increasable’, that is, every time it’s accessed or ‘advanced’ it must be unique from all the identity values before it.  Counting up is a very easy way to accomplish this.

The class also defines an object property $identityPrefix that allows you to place a string prefix in front of the $identityCounter.  As long as the string prefix is unique to a runtime (you can use the class name) the prefix for a particular datum will be globally unique.  These value types are optional.  You can override the ::getNewIdentity() in order to account for different value types (such as two objects, or otherwise).

This particular feature comes in handy when dealing with nodes in a graph or tree to differentiate each node.  It is also handy to return an identity when a function ‘inserts’ or ‘gives’ a piece of data to another object, such as a set.

View on GitHub

This is part of the Phabstractic Library.

photo credit: Don’t Waste You via photopin (license)

Asher Wolfstein

Metaverse Resident

About the Author

A metaverse resident, you can find me on Second Life (kadar.talbot) and other online platforms. I write about my digital life, my musings, and my projects as a programmer, webmaster, artist, and game designer. (exist (be wunk) (use rational imagination) (import artist coder maker furry) (conditional (if (eq you asshole) (me (block you))))

View Articles