Everything lives in one class:
use Webpatser\Uuid\Uuid;
Methods marked #[\NoDiscard] in the source trigger a PHP warning when the return value is ignored.
Generation
| Method | Returns | Description |
|---|---|---|
Uuid::generate(int $ver = 1, mixed $node = null, ?string $ns = null) |
Uuid |
Generate version 1, 3, 4, 5, 6, 7 or 8 |
Uuid::v4() |
Uuid |
Random UUID, same as generate(4) |
Uuid::v7() |
Uuid |
Time-ordered UUID, same as generate(7) |
Uuid::randomBytes(int $bytes) |
string |
Random bytes from Random\Randomizer |
Uuid::nil() |
Uuid |
The nil UUID, all zeros |
generate()
The meaning of $node and $ns depends on the version:
| Version | Call | $node |
$ns |
|---|---|---|---|
| 1 | Uuid::generate(1) |
optional node | unused |
| 3 | Uuid::generate(3, 'example.com', Uuid::NS_DNS) |
name, required | namespace UUID string, required |
| 4 | Uuid::generate(4) |
unused | unused |
| 5 | Uuid::generate(5, 'example.com', Uuid::NS_DNS) |
name, required | namespace UUID string, required |
| 6 | Uuid::generate(6) |
optional node | unused |
| 7 | Uuid::generate(7) |
unused | unused |
| 8 | Uuid::generate(8) |
optional custom data | unused |
$v3 = Uuid::generate(3, 'example.com', Uuid::NS_DNS); // 9073926b-929f-31c2-abc9-fad77ae3e8eb$v5 = Uuid::generate(5, 'example.com', Uuid::NS_DNS); // cfbff0d1-9375-5685-968c-48ce8b15ae17
For version 8, $node is the custom data. With null the UUID is random. A 16-byte string is used as the payload. Any other value is serialized and hashed with SHA-256, so the same data gives the same UUID. In all cases the version and variant bits are set on top of the data.
generate() throws an Exception for:
- version
2(Version 2 is unsupported.) - any other unknown version (
Selected version is invalid or unsupported.) - version 3 or 5 with an empty name (
A name-string is required for Version 3 or 5 UUIDs.). If the name or namespace is null, PHP throws aTypeErrorinstead. - version 3 or 5 with a namespace that is not a UUID (
A binary namespace is required for Version 3 or 5 UUIDs.)
Monotonic v7
Within the same millisecond, v7() embeds a counter in the bits after the timestamp, which keeps up to 256 UUIDs per millisecond from one process in order (8 counter bits survive the version nibble). Order is not guaranteed beyond that or across processes.
Import and validation
| Method | Returns | Description |
|---|---|---|
Uuid::import(string $uuid) |
Uuid |
Build a Uuid from a string or 16 bytes |
Uuid::validate(mixed $uuid) |
bool |
True for a hyphenated 36-character UUID string or a Uuid object |
Uuid::compare(string $a, string $b) |
bool |
Compare two UUIDs, case-insensitive; false if either is invalid |
Uuid::isNilUuid(mixed $uuid) |
bool |
True if the value is the nil UUID |
import() accepts the hyphenated form, a form without hyphens, a urn:uuid: prefix and a 16-byte binary string. It throws a TypeError when the input is none of these.
Uuid::validate('550e8400-e29b-41d4-a716-446655440000'); // trueUuid::validate('550e8400e29b41d4a716446655440000'); // false, no hyphensUuid::import('550e8400e29b41d4a716446655440000'); // worksUuid::compare('550E8400-E29B-41D4-A716-446655440000', '550e8400-e29b-41d4-a716-446655440000'); // trueUuid::compare('not-a-uuid', 'not-a-uuid'); // false
compare() uses hash_equals() on the binary values. Two invalid strings never compare as equal.
Properties
Properties are read through __get(). Unknown names return null.
| Property | Type | Description |
|---|---|---|
string |
string |
Hyphenated lowercase form |
hex |
string |
32 hex characters |
bytes |
string |
16-byte binary string |
urn |
string |
urn:uuid: followed by the string form |
version |
int |
Version number |
variant |
int |
0 (NCS), 1 (RFC 4122), 2 (Microsoft) or 3 (reserved) |
time |
float|null |
Unix timestamp in seconds, for v1, v6 and v7 only |
node |
string|null |
Hex node, for v1 and v6 only |
uuid_ordered |
string |
16 hex characters: the time fields only, reordered as time-high, time-mid, time-low (bytes 6-7, 4-5, 0-3). Clock sequence and node are not included. |
(string) $uuid and $uuid->__toString() return the same value as string.
Instance methods
| Method | Description |
|---|---|
$uuid->__toUuidOrdered() |
Same value as the uuid_ordered property |
$uuid->isNil() |
True if the UUID is the nil UUID |
$uuid->toSqlServer() |
SQL Server GUID string, see SQL Server |
$uuid->toSqlServerBinary() |
16-byte SQL Server binary, see SQL Server |
SQL Server
| Method | Description |
|---|---|
Uuid::importFromSqlServer(string $sqlServerGuid) |
Import a GUID read from a uniqueidentifier column |
Benchmark
$result = Uuid::benchmark(10000, 7);
Uuid::benchmark(int $iterations = 10000, int $version = 7): array returns version, iterations, total_time_ms, avg_time_us, memory_used_bytes and uuids_per_second. See Performance.
Constants
| Constant | Value |
|---|---|
Uuid::NS_DNS |
6ba7b810-9dad-11d1-80b4-00c04fd430c8 |
Uuid::NS_URL |
6ba7b811-9dad-11d1-80b4-00c04fd430c8 |
Uuid::NS_OID |
6ba7b812-9dad-11d1-80b4-00c04fd430c8 |
Uuid::NS_X500 |
6ba7b814-9dad-11d1-80b4-00c04fd430c8 |
Uuid::NIL |
00000000-0000-0000-0000-000000000000 |
Uuid::VALID_UUID_REGEX |
Pattern used by validate(), without delimiters |
The class also defines MD5 (3), SHA1 (5), VERSION_1 to VERSION_8 and the variant and bit-mask constants (VAR_RFC, VAR_MS, VAR_RES, VAR_NCS, CLEAR_VER, CLEAR_VAR, INTERVAL) used internally.
Interactive API testing
Enter a UUID to see the live result of each call below: validation, import, the properties, the nil check, comparison, the SQL Server round trip, and the namespace constants.
| Call | Live output |
|---|---|
Uuid::validate($input) |
true |
$uuid = Uuid::import($input); $uuid->string |
'01a23843-33e1-7006-9170-5edd07df04be' |
$uuid->hex |
'01a2384333e1700691705edd07df04be' |
$uuid->urn |
'urn:uuid:01a23843-33e1-7006-9170-5edd07df04be' |
$uuid->version |
7 |
$uuid->variant |
1 |
$uuid->time |
1796240258.017 |
$uuid->node |
null |
$uuid->uuid_ordered |
'700633e101a23843' |
bin2hex($uuid->bytes) |
'01a2384333e1700691705edd07df04be' |
$uuid->isNil() |
false |
Uuid::isNilUuid($input) |
false |
Uuid::compare($input, strtoupper($input)) |
true |
$uuid->toSqlServer() |
'4338A201-E133-0670-9170-5EDD07DF04BE' |
Uuid::importFromSqlServer($uuid->toSqlServer())->string |
'01a23843-33e1-7006-9170-5edd07df04be' |
Uuid::generate(3, 'example', Uuid::NS_DNS) |
'c5e5f349-28ef-3f5a-98d6-0b32ee4d1743' |
Uuid::generate(5, 'example', Uuid::NS_DNS) |
'7cb48787-6d91-5b9f-bc60-f30298ea5736' |
Uuid::nil() |
'00000000-0000-0000-0000-000000000000' |
Uuid::validate('not-a-uuid') |
false |
Uuid::NS_DNS |
'6ba7b810-9dad-11d1-80b4-00c04fd430c8' |
Uuid::NS_URL |
'6ba7b811-9dad-11d1-80b4-00c04fd430c8' |
Uuid::NS_OID |
'6ba7b812-9dad-11d1-80b4-00c04fd430c8' |
Uuid::NS_X500 |
'6ba7b814-9dad-11d1-80b4-00c04fd430c8' |
Errors
try { $uuid = Uuid::generate(5, 'name', 'invalid-namespace');} catch (Exception $e) { echo $e->getMessage(); // A binary namespace is required for Version 3 or 5 UUIDs.} try { $uuid = Uuid::import('not-a-uuid');} catch (TypeError $e) { echo "Not a UUID";}
Validate input first when you do not control it:
if (Uuid::validate($input)) { $uuid = Uuid::import($input);}
Standards
- RFC 4122: versions 1, 3, 4, 5
- RFC 9562: versions 6, 7, 8