UUID Library v1.3.0

Quick Start Guide

webpatser@dev: ~/uuid/1.3.0 $ cat quick-start.md

Quick Start - UUID Library v1.3.0

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

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 Generation Comparison

UUID v4 (Random)

c7a127e9-b42c-4a99-ac28-6e71dbddaa37
411eae54-054b-41a2-bb5f-18c7ae9d56e9
8632f9f9-27a8-4111-8e44-6efd9b53547c

Notice: Completely random

UUID v7 (Timestamp)

019c26da-4d65-7009-a972-24dabda4f177
019c26da-4d65-7014-a376-c963877c4428
019c26da-4d65-7025-8343-dd31629a31e7

Notice: Sequential ordering

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 Example
v1 Time + MAC Legacy systems c2f09df0-017f-11f1-84af-bdc8c059565a
v4 Random General purpose ✓ 8bb953a8-1b83-4ebd-b048-dab1d79c6233
v7 Timestamp Database optimized ✓ 019c26da-4d65-703d-adf7-3bbfe46d1566

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)

Live Name-Based Example

Input: "example.com" → cfbff0d1-9375-5685-968c-48ce8b15ae17
Input: "example.com" → cfbff0d1-9375-5685-968c-48ce8b15ae17

Same input produces identical UUIDs (deterministic)

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