Ramsey UUID Adds Support for v8

News

November 8th, 2022

Ramsey UUID Adds Support for v8

The popular PHP UUID library by Ben Ramsey released a new version with support for UUID v8, custom UUIDs:

The UUID package implements the UUID v8 draft spec, which outlines a few potentials reasons one might consider using UUID v8:

  • An implementation would like to embed extra information within the UUID other than what is defined in this document.
  • An implementation has other application/language restrictions which inhibit the use of one of the current UUIDs.

Here's an example of generating a version 8 custom UUID:

1use Ramsey\Uuid\Uuid;
2 
3/*
4The bytes should be a 16-byte octet string, an open blob of data that you may fill with 128 bits of information. However, bits 48 through 51 will be replaced with the UUID version field, and bits 64 and 65 will be replaced with the UUID variant. You must not rely on these bits for your application needs.
5*/
6 
7$uuid = Uuid::uuid8("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff");
8 
9printf(
10 "UUID: %s\nVersion: %d\n",
11 $uuid->toString(),
12 $uuid->getFields()->getVersion()
13);
14 
15/*
16UUID: 00112233-4455-8677-8899-aabbccddeeff
17Version: 8
18*/

To learn more about using version 8 UUIDs, read Version 8: Custom in the ramsey/uuid Manual. You can read more about UUID v8 draft spec if you'd like more technical details.

Note: the Laravel framework v9 requires ramsey/uuid:^4.2.2 and you can update dependencies to get the latest version of ramsey/uuid if you'd like to experiment further:

tinker example of uuidv8

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.