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

UUID Library 2.x

Installation

Install webpatser/uuid 2.x, a pure PHP library for UUID versions 1, 3, 4, 5, 6, 7 and 8.

webpatser/uuid generates and validates UUIDs according to RFC 4122 and RFC 9562. It supports versions 1, 3, 4, 5, 6, 7 and 8, converts SQL Server GUID byte order, orders version 7 UUIDs from one process within a millisecond (up to 256 per millisecond), and uses Random\Randomizer and readonly properties.

composer require webpatser/uuid

Requirements

  • PHP ^8.5
  • No PHP extensions
  • No Composer dependencies

Note: Version 2.x requires PHP 8.5 because it uses #[\NoDiscard] and array_first(). On PHP 8.2 to 8.4, stay on 1.x.

Verify the install

use Webpatser\Uuid\Uuid;
 
echo Uuid::v4();

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
    0204d8c2-789c-465b-99bd-e7c6b1c35f55
  • Uuid::v7() v7, time-ordered
    01a23843-3a6f-7000-b21a-d94f251d9173
  • Uuid::generate(1) v1, time and node
    d59beb50-ee87-11f1-b7c1-a712e4171ee9
  • 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-59be-6bc0-bbdc-873dfa442890
  • Uuid::generate(8) v8, custom
    7b71ff54-0117-885c-a5b0-7feba618b123
  • Uuid::nil() Nil
    00000000-0000-0000-0000-000000000000

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

This demo runs webpatser/uuid 3.0.0, the version installed on this site.

Upgrading from 1.x

See Upgrading from 1.x for the breaking changes.

Laravel

If you use Laravel and want automatic model UUIDs, see the Laravel UUID package.

Next steps