PHPackages                             phossa2/uuid - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. phossa2/uuid

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

phossa2/uuid
============

A PHP lib to generate sequential/time based uuid for using as PK in DB.

2.0.0(9y ago)52.5k21MITPHPPHP ~5.4|~7.0

Since Sep 20Pushed 9y ago2 watchersCompare

[ Source](https://github.com/phossa2/uuid)[ Packagist](https://packagist.org/packages/phossa2/uuid)[ Docs](https://github.com/phossa2/uuid)[ RSS](/packages/phossa2-uuid/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (1)

phossa2/uuid
============

[](#phossa2uuid)

[![Build Status](https://camo.githubusercontent.com/79a1fdfb3f153f8a66a69d1651a362894eb3c8d8eb9aa4a53390db01e1e166d9/68747470733a2f2f7472617669732d63692e6f72672f70686f737361322f757569642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/phossa2/uuid)[![Code Quality](https://camo.githubusercontent.com/6c07a374823ec363411daf92f776eb1051fd341bc8d375a9a0b343ec89a3fb83/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f70686f737361322f757569642f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/phossa2/uuid/)[![Code Climate](https://camo.githubusercontent.com/35bc91ac8baa0c7f8f34efdea0af580955c8872aec38325403142163ff4aa439/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f70686f737361322f757569642f6261646765732f6770612e737667)](https://codeclimate.com/github/phossa2/uuid)[![PHP 7 ready](https://camo.githubusercontent.com/54d4f8c96a0cdb61948cedf5d03f73413c98af137f4572a8a532c91353cefccb/687474703a2f2f7068703772656164792e74696d6573706c696e7465722e63682f70686f737361322f757569642f6d61737465722f62616467652e737667)](https://travis-ci.org/phossa2/uuid)[![HHVM](https://camo.githubusercontent.com/4dd577748e5a9add03b1c27d82a3d9492ad81e6539c87cd02fe58edf6973b055/68747470733a2f2f696d672e736869656c64732e696f2f6868766d2f70686f737361322f757569642e7376673f7374796c653d666c6174)](http://hhvm.h4cc.de/package/phossa2/uuid)[![Latest Stable Version](https://camo.githubusercontent.com/db35520e2829b66dc28f44073c5fd6bc5483b15c05fb9f3c91364f742bc82b91/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f70686f737361322f757569642e7376673f7374796c653d666c6174)](https://packagist.org/packages/phossa2/uuid)[![License](https://camo.githubusercontent.com/4fc6538ded72843e26a272d300ce4c95da083ce92576e10e4fdd505d579a8125/68747470733a2f2f696d672e736869656c64732e696f2f3a6c6963656e73652d6d69742d626c75652e737667)](http://mit-license.org/)

**phossa2/uuid** is a PHP library for generating sequential UUID to be used as primary key in databases.

It requires PHP 5.4, supports PHP 7.0+ and HHVM. It is compliant with [PSR-1](http://www.php-fig.org/psr/psr-1/ "PSR-1: Basic Coding Standard"), [PSR-2](http://www.php-fig.org/psr/psr-2/ "PSR-2: Coding Style Guide"), [PSR-3](http://www.php-fig.org/psr/psr-3/ "PSR-3: Logger Interface"), [PSR-4](http://www.php-fig.org/psr/psr-4/ "PSR-4: Autoloader"), and the proposed [PSR-5](https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md "PSR-5: PHPDoc").

Installation
------------

[](#installation)

Install via the `composer` utility.

```
composer require "phossa2/uuid"
```

or add the following lines to your `composer.json`

```
{
    "require": {
       "phossa2/uuid": "2.*"
    }
}
```

Features
--------

[](#features)

- **Ordered UUID**

    According to article [Store UUID in an optimized way](https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/), Non-ordered UUID has big impact on Mysql db insert performance.
- **Typed UUID**

    Instead of following RFC 4122 for generating UUID, we adopted a new design with data types built in. For example, user id has the type of `1010`. And any user id using this lib will start with '2101-0'
- **Sharding supported**

    With sharding bits built-in, it is easy to shard your db tables.
- **Ready for extension**

    As long as the timestamp algorithm is good enough, it will guarantee uniqueness at least inside one vendor's house.

Design
------

[](#design)

Using 32 chars, without `-`

```
 2xxx - xxxx - xxxx - xxxx - xxxx - xxxx - xxxx - xxxx
 ^ ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^   ^^^^   ^^^^ ^^^^^^
ver type          timestamp         shard  vendor remain

```

- version: position 0, 1 char

    - this uuid lib version
    - default to `2`
- data type: position 1 - 4, 4 chars

    - 16bit, 65535
    - lib reserves types `1***`
    - custom types starts from `[2-f]***`
- timestamp: position 5 - 19, 15 chars

    - 60bit
    - can be used for at least 360 years
- shard: position 20 - 23, 4 chars

    - 16bit, 65535
    - for sharding purpose, provided by user
- vendor: position 24 - 27 (4 chars)

    - vendor id provided by user
- remain: position 28 - 31 (4 chars)

    - reserved for future usage

Usage
-----

[](#usage)

```
use Phossa2\Uuid\Uuid;

// 2100020bc58eb7f18602000100010000
$uuid = Uuid::get();

// encode/shorten it, can be used in URL
if (Uuid::isValid($uuid)) {
    // AWprUw7urpN8bbQ4LciGNa
    $short = Uuid::encode($uuid);

    // decode
    var_dump($uuid === Uuid::decode($short)); // true
}
```

Extend `Phossa2\Uuid\Uuid` with your own settings or algorithm,

```
class MyUuid extends Uuid
{
    /*
     * use this vendor id
     *
     * {@inheritDoc}
     */
    protected $vendor = '1234';

    /*
     * use this more reliable sequence
     *
     * {@inheritDoc}
     */
    protected function getSequence()
    {
         // ...
    }
}
```

APIs
----

[](#apis)

- `UuidInterface`

    - `Uuid::get(string $dataType, string $shardId): string`

        Both parameters are optional.
- `UtilityInterface`

    - `Uuid::isValid(string $uuid): bool`

        Check `$uuid` valid or not.
    - `Uuid::info(string $uuid): array`

        Get detail information about this `$uuid` including `version`, `type`, `time`, `vendor`, `remain`.
    - `Uuid::encode(string $uuid): string`

        Encode `$uuid` into a short version (base56)
    - `Uuid::decode(string $string): string`

        Decode the short version into full 32-char UUID

Predefined data types
---------------------

[](#predefined-data-types)

- Generic OID `UuidInterface::TYPE_OID`, value `1000`.
- User id `UuidInterface::TYPE_USER`, value `1010`.
- Post or article `UuidInterface::TYPE_POST`, value `1020`.
- News `UuidInterface::TYPE_NEWS`, value `1021`.
- Image `UuidInterface::TYPE_IMAGE`, value `1030`.
- Image album `UuidInterface::TYPE_ALBUM`, value `1031`.
- Comment `UuidInterface::TYPE_COMM`, value `1040`.
- Rating `UuidInterface::TYPE_RATE`, value `1041`.

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) from more information.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTE](CONTRIBUTE.md) for more information.

Dependencies
------------

[](#dependencies)

- PHP &gt;= 5.4.0
- phossa2/shared &gt;= 2.0.21

License
-------

[](#license)

[MIT License](http://mit-license.org/)

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3528d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6e0ecf2ab11402101d86a36b457973b81149757a53a67dd048bbfb9025890abf?d=identicon)[phossa2](/maintainers/phossa2)

---

Top Contributors

[![phossa](https://avatars.githubusercontent.com/u/8499165?v=4)](https://github.com/phossa "phossa (22 commits)")[![phossa2](https://avatars.githubusercontent.com/u/19922046?v=4)](https://github.com/phossa2 "phossa2 (1 commits)")

---

Tags

uuidphossa

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/phossa2-uuid/health.svg)

```
[![Health](https://phpackages.com/badges/phossa2-uuid/health.svg)](https://phpackages.com/packages/phossa2-uuid)
```

###  Alternatives

[ramsey/uuid

A PHP library for generating and working with universally unique identifiers (UUIDs).

12.6k700.2M3.3k](/packages/ramsey-uuid)[symfony/polyfill-uuid

Symfony polyfill for uuid functions

688335.4M63](/packages/symfony-polyfill-uuid)[symfony/uid

Provides an object-oriented API to generate and represent UIDs

610280.0M754](/packages/symfony-uid)[webpatser/laravel-uuid

Laravel integration for webpatser/uuid - High-performance drop-in UUID replacements (15% faster than Ramsey). Provides Str macros, HasUuids trait, facades, and casts. RFC 4122/9562 compliant.

1.8k17.3M129](/packages/webpatser-laravel-uuid)[pascaldevink/shortuuid

PHP 7.4+ library that generates concise, unambiguous, URL-safe UUIDs

5951.8M15](/packages/pascaldevink-shortuuid)[emadadly/laravel-uuid

laravel uuid a simple, automatic UUID generator for any model based on Laravel.

120415.9k3](/packages/emadadly-laravel-uuid)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
