Phabstractic: Identity Trait Feature
asherwunk/phabstractic now has a feature for classes for generating unique identities: Identity Trait
This is a very straightforward feature:
protected static $identityCounter = 0;
protected $identityPrefix = '';
protected function getNewIdentity()
return $this->identityPrefix . static::$identityCounter;
The feature defines a static property for the using 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 its 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.
This is part of the Phabstractic Library.
If you appreciate my programming please help support me through my Patreon.