Everything is on Webpatser\Uuid\Uuid. The class is not final, but its constructor is protected: create instances through the static methods.
Generation
| Method | Description |
|---|---|
Uuid::v4(): self |
Random UUID (version 4) |
Uuid::v7(): self |
Time-ordered UUID (version 7) |
Uuid::generate(int $ver = 1, mixed $node = null, ?string $ns = null): self |
Any version: 1, 3, 4, 5, 6, 7, 8 |
Uuid::nil(): self |
The nil UUID, all zeros |
generate() arguments by version:
| Version | $node |
$ns |
|---|---|---|
| 1, 6 | Optional MAC address, 6 raw bytes or hex (separators are stripped). A random node with the multicast bit set is used when omitted or invalid | Unused |
| 3, 5 | Required name string | Required namespace UUID as a string (for example Uuid::NS_DNS or (string) $uuid) |
| 4, 7 | Unused | Unused |
| 8 | Optional: a 16-byte string is used as is, any other value is hashed with SHA-256 over serialize(), null gives random data |
Unused |
use Webpatser\Uuid\Uuid; $v1 = Uuid::generate(1, '00:1B:44:11:3A:B7');echo $v1->node; // 001b44113ab7 $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
Errors: version 2 throws Exception('Version 2 is unsupported.'). Any other unknown version throws Exception('Selected version is invalid or unsupported.'). A version 3 or 5 call without a name or namespace throws a TypeError or an Exception.
Version 7 UUIDs from one process are ordered within one millisecond through a counter in the bits after the timestamp (8 bits survive the version nibble, so order holds for up to 256 UUIDs per millisecond; across processes it is not guaranteed).
Import and validation
| Method | Description |
|---|---|
Uuid::import(string $uuid): self |
Accepts a hyphenated or plain hex string, a urn:uuid: string, or 16 raw bytes |
Uuid::validate(mixed $uuid): bool |
true only for the hyphenated 36-character form |
Uuid::compare(string $a, string $b): bool |
Case-insensitive, constant-time comparison. false if either side is not a UUID |
Uuid::isNilUuid(mixed $uuid): bool |
Whether a Uuid or string is the nil UUID |
$uuid->isNil(): bool |
Whether this instance is the nil UUID |
import() throws a TypeError when the input is neither 16 bytes nor 32 hex digits after stripping urn:uuid: and non-hex characters. Any 16-character string is taken as raw bytes. Check with validate() first when the input is untrusted.
Uuid::validate('6ba7b810-9dad-11d1-80b4-00c04fd430c8'); // trueUuid::validate('6ba7b8109dad11d180b400c04fd430c8'); // false, no hyphensUuid::validate(null); // false Uuid::compare('not', 'valid'); // false
Properties
Instances expose read-only magic properties.
| Property | Type | Description |
|---|---|---|
string |
string |
Hyphenated lowercase form |
hex |
string |
32 hex characters |
bytes |
string |
16 raw bytes |
urn |
string |
urn:uuid: plus the string form |
version |
int |
Version number |
variant |
int |
0 NCS, 1 RFC 4122, 2 Microsoft, 3 reserved |
time |
float|null |
Unix timestamp for versions 1, 6, 7, otherwise null |
node |
string|null |
Node hex for versions 1 and 6, otherwise null |
uuid_ordered |
string |
16 hex characters: only the time fields, reordered (time high, time mid, time low); clock sequence and node are not included |
An instance casts to its string form: (string) $uuid. Unknown property names return null.
SQL Server
| Method | Description |
|---|---|
Uuid::importFromSqlServer(string $sqlServerGuid): self |
Import a SQL Server GUID, correcting byte order. Throws Exception('Invalid SQL Server GUID format.') |
$uuid->toSqlServer(): string |
Uppercase GUID string in SQL Server byte order |
$uuid->toSqlServerBinary(): string |
16 bytes in SQL Server byte order |
See Microsoft SQL Server.
Benchmark
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 |
^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$ |
The class also defines MD5 (3), SHA1 (5), the VERSION_1 to VERSION_8 and VAR_* bit constants, and INTERVAL, the Gregorian-to-Unix offset in 100 ns units.
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-332b-7007-9a0a-c04d72881da6' |
$uuid->hex |
'01a23843332b70079a0ac04d72881da6' |
$uuid->urn |
'urn:uuid:01a23843-332b-7007-9a0a-c04d72881da6' |
$uuid->version |
7 |
$uuid->variant |
1 |
$uuid->time |
1796240257.835 |
$uuid->node |
null |
$uuid->uuid_ordered |
'7007332b01a23843' |
bin2hex($uuid->bytes) |
'01a23843332b70079a0ac04d72881da6' |
$uuid->isNil() |
false |
Uuid::isNilUuid($input) |
false |
Uuid::compare($input, strtoupper($input)) |
true |
$uuid->toSqlServer() |
'4338A201-2B33-0770-9A0A-C04D72881DA6' |
Uuid::importFromSqlServer($uuid->toSqlServer())->string |
'01a23843-332b-7007-9a0a-c04d72881da6' |
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' |
This demo runs webpatser/uuid 3.0.0, the version installed on this site.
Utility
Uuid::randomBytes(int $bytes): string returns random bytes from Random\Randomizer.
NoDiscard
generate(), v4(), v7(), import(), compare(), validate(), nil(), benchmark(), importFromSqlServer(), toSqlServer() and toSqlServerBinary() carry #[\NoDiscard].