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

UUID Library 1.x

Quick Start

Get started with high-performance UUID generation in minutes, covering the most common use cases.

Get started with high-performance UUID generation in minutes. This guide covers the most common use cases.

Basic UUID Generation

The most common UUIDs you'll need:

use Webpatser\Uuid\Uuid;
 
// Random UUID (most common)
$uuid4 = Uuid::v4();
echo $uuid4; // e.g., "550e8400-e29b-41d4-a716-446655440000"
 
// Timestamp-based UUID (database optimized)
$uuid7 = Uuid::v7();
echo $uuid7; // e.g., "018f4e7c-8b3a-7000-8000-123456789abc"

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
    da955fb5-e762-4aa3-a865-1a41e281177f
  • Uuid::v7() v7, time-ordered
    01a23843-3b1e-7007-bbc2-2bd3b39b979f
  • Uuid::generate(1) v1, time and node
    d5b69410-ee87-11f1-9d54-676c99f4eac2
  • 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-5b69-6480-893f-1fb65bb838fa
  • Uuid::generate(8) v8, custom
    6fa86e0e-91ca-8e59-9a8f-4bf7c47f4c94
  • 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.

Working with UUIDs

Access different formats and validate UUIDs:

$uuid = Uuid::v4();
 
// Different formats
echo $uuid->string; // "550e8400-e29b-41d4-a716-446655440000"
echo $uuid->hex; // "550e8400e29b41d4a716446655440000"
echo $uuid->bytes; // Raw 16-byte binary data
 
// Validation
$isValid = Uuid::validate('550e8400-e29b-41d4-a716-446655440000'); // true
$isValid = Uuid::validate('invalid-uuid'); // false
 
// Import existing UUIDs
$imported = Uuid::import('550e8400-e29b-41d4-a716-446655440000');

UUID Versions Explained

Version Type Use Case
v1 Time + MAC Legacy systems
v4 Random General purpose
v7 Timestamp Database optimized

Name-Based UUIDs

Generate deterministic UUIDs from names:

// MD5-based (v3)
$uuid3 = Uuid::generate(3, 'example.com', Uuid::NS_DNS);
 
// SHA-1-based (v5) - recommended
$uuid5 = Uuid::generate(5, 'example.com', Uuid::NS_DNS);
 
// Same input = same UUID
$same = Uuid::generate(5, 'example.com', Uuid::NS_DNS);
// $uuid5 === $same (true)

Same input produces identical UUIDs (deterministic).

Try it live

Pick a version and, for v3 and v5, a name and a namespace. The generator runs the matching Uuid::generate() call on this server and lets you copy the result.

Version
Uuid::v4()
b0e629cf-6724-4588-b195-b7aeee2f6680

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

Performance Tips

  • Use v4 for general purpose: Maximum randomness, good performance
  • Use v7 for databases: Natural sorting order improves indexing
  • Batch operations: Generate multiple UUIDs in loops efficiently
  • Cache when possible: Store UUIDs rather than regenerating
// Efficient batch generation
$uuids = [];
for ($i = 0; $i < 1000; $i++) {
$uuids[] = Uuid::v7(); // Maintains chronological order
}
 
// Benchmark different versions
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
Uuid::v4();
}
$time = (microtime(true) - $start) * 1000;
echo "Generated 10k v4 UUIDs in {$time}ms";

Next Steps