webpatser/uuid
Composer 安装命令:
composer require webpatser/uuid
包简介
A pure PHP library to generate and validate universally unique identifiers (UUIDs) according to RFC 4122 and RFC 9562 standards. Support for UUID versions 1, 3, 4, 5, 6, 7, and 8.
README 文档
README
A pure PHP library to generate and validate universally unique identifiers (UUIDs) according to RFC 4122 and RFC 9562 standards. Supports UUID versions 1, 3, 4, 5, 6, 7, and 8.
Requirements: PHP ^8.5 (no extensions required)
Installation
composer require webpatser/uuid
Quick Start
use Webpatser\Uuid\Uuid; // Generate UUIDs $uuid4 = Uuid::v4(); // Random (recommended for general use) $uuid7 = Uuid::v7(); // Time-ordered (recommended for databases) $uuid1 = Uuid::generate(1); // Time-based with MAC address $uuid3 = Uuid::generate(3, 'example.com', Uuid::NS_DNS); // Name-based (MD5) $uuid5 = Uuid::generate(5, 'example.com', Uuid::NS_DNS); // Name-based (SHA-1) $uuid6 = Uuid::generate(6); // Reordered time-based $uuid8 = Uuid::generate(8); // Custom/vendor-specific echo (string) $uuid4; // e.g., "550e8400-e29b-41d4-a716-446655440000"
API Reference
Generation
| Method | Description |
|---|---|
Uuid::v4() |
Generate random UUID (version 4) |
Uuid::v7() |
Generate time-ordered UUID (version 7) |
Uuid::generate(int $ver, mixed $node = null, ?string $ns = null) |
Generate any version (1, 3, 4, 5, 6, 7, 8) |
Uuid::nil() |
Create nil UUID (all zeros) |
Import & Validation
| Method | Description |
|---|---|
Uuid::import(string $uuid) |
Import a UUID string |
Uuid::validate(mixed $uuid) |
Check if string is valid UUID format |
Uuid::compare(string $a, string $b) |
Compare two UUIDs (case-insensitive) |
Uuid::isNilUuid(mixed $uuid) |
Check if UUID is nil |
Properties
$uuid = Uuid::v7(); $uuid->string; // "019d05e8-dfa1-7009-a8f7-4e1c868ccfa4" $uuid->hex; // "019d05e8dfa17009a8f74e1c868ccfa4" $uuid->bytes; // 16-byte binary string $uuid->urn; // "urn:uuid:019d05e8-dfa1-7009-a8f7-4e1c868ccfa4" $uuid->version; // 7 $uuid->variant; // 1 (RFC 4122) $uuid->time; // 1773920640.936 (Unix timestamp, V1/V6/V7 only) $uuid->node; // MAC address hex (V1/V6 only, null for others)
SQL Server GUID Support
SQL Server stores GUIDs with mixed endianness. These methods handle the byte-order conversion:
// Import from SQL Server $uuid = Uuid::importFromSqlServer('825B076B-44EC-E511-80DC-00155D0ABC54'); // Export to SQL Server format $sqlGuid = $uuid->toSqlServer(); // String format $sqlBin = $uuid->toSqlServerBinary(); // 16-byte binary
Benchmarking
$result = Uuid::benchmark(10000, 7); // Returns: version, iterations, total_time_ms, avg_time_us, memory_used_bytes, uuids_per_second
Run the benchmark script to compare versions:
php examples/benchmark.php 10000
Name-Based UUID Namespaces
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
Features
- UUID versions 1, 3, 4, 5, 6, 7, and 8 (RFC 4122 + RFC 9562)
- SQL Server GUID byte-order conversion
- Monotonic V7 UUIDs with 12-bit sub-millisecond sequence
#[\NoDiscard]attribute on all factory and query methodsarray_first()PHP 8.5 native functionRandom\Randomizerfor cryptographic random bytesreadonlyproperties for immutabilitymatchexpressions for efficient dispatching- ~40% faster than ramsey/uuid on V4, ~45% on V7
- Zero dependencies, pure PHP
License
MIT License.
webpatser/uuid 适用场景与选型建议
webpatser/uuid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 80.78k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「random」 「uuid」 「identifier」 「guid」 「rfc4122」 「rfc9562」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webpatser/uuid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webpatser/uuid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webpatser/uuid 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Paramconverter, Normalizer and Form Type for Ramsey Uuid
A PHP library for generating and working with identifiers, including UUIDs, ULIDs, and Snowflakes
DrUUID RFC 4122 library for PHP
Trait to generate uuid when creating models
Faker Japanese is a Faker provider that generates fake Japanese related data for you.
Gambling Algorithms for Certification.
统计信息
- 总下载量: 80.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 31
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-10