~/docs
Resonate
Resonate
4.x 4.0.0, current 3.x 3.0.1 2.x 2.0.1 1.x 1.3.0
UUID Library
Toggle sidebar
UUID Library
webpatser/uuid 4.0.0

UUID Library 4.x

Installation

Install webpatser/uuid 4.0, a pure PHP library that generates UUID versions 1, 3, 4, 5, 6, 7 and 8 according to RFC 4122 and RFC 9562.

webpatser/uuid is a pure PHP library that generates and validates universally unique identifiers (UUIDs) according to RFC 4122 and RFC 9562. It supports versions 1, 3, 4, 5, 6, 7 and 8 and has no runtime dependencies.

composer require webpatser/uuid:^4.0

Requirements

  • PHP 8.6 or higher ("php": "^8.6" in composer.json)
  • No extensions and no other packages

PHP 8.5 is not supported by 3.x or 4.x. If you run PHP 8.5, stay on 2.x. See Upgrading from 2.x and Upgrading from 3.x.

Namespace

The package registers the Webpatser\Uuid\ namespace (PSR-4, src/). The library is a single class:

use Webpatser\Uuid\Uuid;

Features

  • UUID versions 1, 3, 4, 5, 6, 7 and 8
  • v7 UUIDs with a per-millisecond counter that keeps UUIDs from one process in order, up to 256 per millisecond
  • Import and validation of UUID strings, URNs and 16-byte binary values
  • SQL Server GUID byte-order conversion
  • #[\NoDiscard] on factory and query methods
  • Randomness from Random\Randomizer

Live output

The block below generates one UUID per version on this server, each next to the call that produced it. Press Generate again for new values. The name-based rows are always the same for the same name and namespace.

Generated on this server for this page view
  • Uuid::v4() v4, random
    0f100b33-1e92-4386-8be0-74d871f03280
  • Uuid::v7() v7, time-ordered
    01a23838-76f9-7003-8e0f-1f33f5c775dc
  • Uuid::generate(1) v1, time and node
    3128a780-ee86-11f1-b4e0-0581b069a330
  • Uuid::generate(3, 'example.com', Uuid::NS_DNS) v3, MD5 name-based
    9073926b-929f-31c2-abc9-fad77ae3e8eb
  • Uuid::generate(5, 'example.com', Uuid::NS_DNS) v5, SHA-1 name-based
    cfbff0d1-9375-5685-968c-48ce8b15ae17
  • Uuid::generate(6) v6, time-ordered (RFC 9562)
    1f1ee863-128a-67f0-ac47-a3ace6f0df05
  • Uuid::generate(8) v8, custom
    ba85c618-9ca5-8c47-906d-fc9a168b5cea
  • Uuid::nil() Nil
    00000000-0000-0000-0000-000000000000

The v3 and v5 values stay the same on every view; the others change each time.

Laravel

Note: Using Laravel? The Laravel UUID package builds on this library with model keys, validation rules and Eloquent support.

Next steps