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]andarray_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.
-
Uuid::v4()v4, random0204d8c2-789c-465b-99bd-e7c6b1c35f55 -
Uuid::v7()v7, time-ordered01a23843-3a6f-7000-b21a-d94f251d9173 -
Uuid::generate(1)v1, time and noded59beb50-ee87-11f1-b7c1-a712e4171ee9 -
Uuid::generate(3, 'example.com', Uuid::NS_DNS)v3, MD5 name-based9073926b-929f-31c2-abc9-fad77ae3e8eb -
Uuid::generate(5, 'example.com', Uuid::NS_DNS)v5, SHA-1 name-basedcfbff0d1-9375-5685-968c-48ce8b15ae17 -
Uuid::generate(6)v6, time-ordered (RFC 9562)1f1ee87d-59be-6bc0-bbdc-873dfa442890 -
Uuid::generate(8)v8, custom7b71ff54-0117-885c-a5b0-7feba618b123 -
Uuid::nil()Nil00000000-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
- Quick Start: generate, read and validate UUIDs.
- Examples: complete scripts.
- API Reference: every public method and constant.