UUID Library v1.3.0
Complete API Reference
API Reference - UUID Library v1.3.0
Complete reference for all UUID generation methods, constants, and utility functions.
Static Generation Methods
v1() - Time-based UUID
use Webpatser\Uuid\Uuid; // Generate v1 UUID (time-based with MAC address)$uuid = Uuid::generate(1);echo $uuid; // e.g., 1ec9414c-232a-6dfa-883c-0242ac130003
Returns: Time-based UUID containing timestamp and MAC address.
Use case: When you need sortable UUIDs with timestamp information.
v3() - Name-based MD5 UUID
use Webpatser\Uuid\Uuid; // Generate v3 UUID (MD5 hash of namespace + name)$uuid = Uuid::generate(3, 'example.com', Uuid::NS_DNS);echo $uuid; // Always: 9073926b-929f-31c2-abc9-fad77ae3e8eb
Parameters: namespace (UUID), name (string)
Returns: Deterministic UUID based on MD5 hash.
Use case: When you need reproducible UUIDs from the same input.
v4() - Random UUID
use Webpatser\Uuid\Uuid; // Generate v4 UUID (completely random)$uuid = Uuid::v4();echo $uuid; // e.g., 3c6e0b8a-b9c6-4c8e-9c4a-8f9e7d6c5b4a
Returns: Cryptographically random UUID.
Use case: General purpose unique identifiers.
v5() - Name-based SHA1 UUID
use Webpatser\Uuid\Uuid; // Generate v5 UUID (SHA1 hash of namespace + name)$uuid = Uuid::generate(5, 'example.com', Uuid::NS_DNS);echo $uuid; // Always: cfbff0d1-9375-5685-968c-48ce8b15ae17
Parameters: namespace (UUID), name (string)
Returns: Deterministic UUID based on SHA1 hash.
Use case: Reproducible UUIDs with better collision resistance than v3.
v6() - Reordered Time UUID
use Webpatser\Uuid\Uuid; // Generate v6 UUID (reordered timestamp for better database performance)$uuid = Uuid::generate(6);echo $uuid; // e.g., 1ec9414c-232a-6dfa-883c-0242ac130003
Returns: Time-based UUID with improved lexicographic sorting.
Use case: Database primary keys requiring chronological sorting.
v7() - Unix Timestamp UUID
use Webpatser\Uuid\Uuid; // Generate v7 UUID (Unix timestamp based)$uuid = Uuid::v7();echo $uuid; // e.g., 018b2c4f-df32-7000-8000-123456789abc
Returns: UUID with Unix timestamp prefix for optimal database sorting.
Use case: Modern applications requiring time-sortable UUIDs.
v8() - Custom UUID
use Webpatser\Uuid\Uuid; // Generate v8 UUID (custom/experimental)$uuid = Uuid::generate(8);echo $uuid; // Implementation-specific format
Returns: Custom format UUID for experimental use.
Use case: Application-specific UUID formats.
Legacy Generation Method
generate() - Version-specific Generation
use Webpatser\Uuid\Uuid; // Generate specific UUID versions$uuid1 = Uuid::generate(1); // v1$uuid3 = Uuid::generate(3, 'example', Uuid::NS_DNS); // v3$uuid4 = Uuid::generate(4); // v4$uuid5 = Uuid::generate(5, 'example', Uuid::NS_DNS); // v5$uuid6 = Uuid::generate(6); // v6$uuid7 = Uuid::generate(7); // v7$uuid8 = Uuid::generate(8); // v8
Special UUIDs
nil() - Nil UUID
use Webpatser\Uuid\Uuid; // Generate nil UUID (all zeros)$uuid = Uuid::nil();echo $uuid; // 00000000-0000-0000-0000-000000000000
Predefined Namespaces
Constant
|
UUID
|
Description
|
---|---|---|
Uuid::NS_DNS | 6ba7b810-9dad-11d1-80b4-00c04fd430c8 | DNS namespace |
Uuid::NS_URL | 6ba7b811-9dad-11d1-80b4-00c04fd430c8 | URL namespace |
Uuid::NS_OID | 6ba7b812-9dad-11d1-80b4-00c04fd430c8 | Object identifier namespace |
Uuid::NS_X500 | 6ba7b814-9dad-11d1-80b4-00c04fd430c8 | X.500 DN namespace |
Utility Methods
validate() - UUID Validation
use Webpatser\Uuid\Uuid; // Validate UUID string format$isValid = Uuid::validate('550e8400-e29b-41d4-a716-446655440000'); // true$isValid = Uuid::validate('invalid-uuid'); // false$isValid = Uuid::validate('550e8400e29b41d4a716446655440000'); // false (no hyphens)
import() - Import UUID
use Webpatser\Uuid\Uuid; // Import existing UUID string$uuid = Uuid::import('550e8400-e29b-41d4-a716-446655440000');echo $uuid->string; // "550e8400-e29b-41d4-a716-446655440000"echo $uuid->hex; // "550e8400e29b41d4a716446655440000"
compare() - UUID Comparison
use Webpatser\Uuid\Uuid; $uuid1 = Uuid::v4();$uuid2 = Uuid::import($uuid1->string); $areEqual = Uuid::compare($uuid1, $uuid2); // true$areEqual = ($uuid1->string === $uuid2->string); // Also true
Instance Properties
$uuid = Uuid::v4(); // Different formatsecho $uuid->string; // "550e8400-e29b-41d4-a716-446655440000"echo $uuid->hex; // "550e8400e29b41d4a716446655440000"echo $uuid->bytes; // Raw 16-byte binary data // UUID componentsecho $uuid->version; // 4echo $uuid->variant; // RFC 4122 variant
Usage Examples
Namespace-based UUIDs
use Webpatser\Uuid\Uuid; // Using predefined namespaces$dnsUuid = Uuid::generate(5, 'example.com', Uuid::NS_DNS);$urlUuid = Uuid::generate(5, 'https://example.com/page', Uuid::NS_URL);$oidUuid = Uuid::generate(5, '1.3.6.1.4.1.343', Uuid::NS_OID); // Using custom namespace$customNamespace = Uuid::v4(); // Generate a random namespace$customUuid = Uuid::generate(5, 'my-custom-name', $customNamespace);
Interactive API Testing
Type
|
Method/Constant
|
Live Output
|
---|---|---|
Namespace |
NS_DNS
|
6ba7b810-9dad-11d1-80b4-00c04fd430c8 |
Namespace |
NS_URL
|
6ba7b811-9dad-11d1-80b4-00c04fd430c8 |
Namespace |
NS_OID
|
6ba7b812-9dad-11d1-80b4-00c04fd430c8 |
Namespace |
NS_X500
|
6ba7b814-9dad-11d1-80b4-00c04fd430c8 |
UUID v3 |
v3('example')
|
c5e5f349-28ef-3f5a-98d6-0b32ee4d1743 |
UUID v5 |
v5('example')
|
7cb48787-6d91-5b9f-bc60-f30298ea5736 |
Nil UUID |
nil()
|
00000000-0000-0000-0000-000000000000 |
Return Types & Format
All UUID generation methods return a string in the standard UUID format:
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx Where:- x = hexadecimal digit (0-9, a-f)- M = UUID version number (1-8)- N = UUID variant bits (8, 9, A, or B)
Standards Compliance
- RFC 4122: Versions 1, 3, 4, 5
- RFC 9562: Versions 6, 7, 8 (draft standard)
- Format: 36-character string with hyphens
- Encoding: Hexadecimal lowercase
Performance Note
This library is 15% faster than Ramsey UUID for most operations, with optimized algorithms and minimal memory footprint.
Error Handling
use Webpatser\Uuid\Uuid;use InvalidArgumentException; try { // Invalid namespace $uuid = Uuid::v5('invalid-namespace', 'name');} catch (InvalidArgumentException $e) { echo "Error: " . $e->getMessage();} try { // Invalid UUID string $uuid = Uuid::import('not-a-uuid');} catch (InvalidArgumentException $e) { echo "Error: " . $e->getMessage();}