~/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 3.0.1
You are reading the 3.x documentation
The current line is 4.x. Read this page for 4.x

UUID Library 3.x

Installation

Install webpatser/uuid 3.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

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. If you run PHP 8.5, stay on 2.x. See Upgrading from 2.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
    552188ac-5a5a-4912-8dad-cb3d43d36b42
  • Uuid::v7() v7, time-ordered
    01a23843-3ba0-7003-aade-21c085effa79
  • Uuid::generate(1) v1, time and node
    d5ca63d0-ee87-11f1-be2b-75abe52cfe21
  • 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)
    1f1ee87d-5ca6-6440-8d48-37243d37e7a5
  • Uuid::generate(8) v8, custom
    b3fe9e08-5421-82b3-bcdd-a6b8db2b373d
  • 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