1. Go to this page and download the library: Download php-architecture-kit/uuid library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
php-architecture-kit / uuid example snippets
use PhpArchitecture\Uuid\Foundation\Uuid;
// Generate a new UUID (defaults to v7)
$uuid = Uuid::new();
// Generate specific versions
$uuidV4 = Uuid::v4(); // Random
$uuidV7 = Uuid::v7(); // Time-ordered (recommended)
$uuidV1 = Uuid::v1(); // Time-based (legacy)
// Deterministic UUIDs
$namespace = Uuid::fromString(Uuid::NAMESPACE_URL);
$uuidV5 = Uuid::v5($namespace, 'https://example.com');
// Parse existing UUID
$uuid = Uuid::fromString('df516cba-fb13-4f45-8335-00252f1b87e2');
$uuid = Uuid::fromBinary($binaryData);
// Use as Value Object
$uuid->value(); // 'df516cba-fb13-4f45-8335-00252f1b87e2'
$uuid->toString(); // 'df516cba-fb13-4f45-8335-00252f1b87e2'
$uuid->toBinary(); // 16 bytes
$uuid->getVersion(); // 4
$uuid->equals($other); // true/false
use PhpArchitecture\Uuid\Foundation\Uuid;
final class UserId extends Uuid
{
public static function new(): static
{
return static::v7(); // Time-ordered for better DB performance
}
}
final class OrderId extends Uuid
{
public static function new(): static
{
return static::v4(); // Random for privacy
}
}
// Usage
$userId = UserId::new();
$orderId = OrderId::fromString('df516cba-fb13-4f45-8335-00252f1b87e2');