Getting Started¶
Requirements¶
ramsey/uuid 4.5.1 requires the following:
PHP 8.0+
ext-ctype or a polyfill that provides ext-ctype, such as symfony/polyfill-ctype
The JSON extension is normally enabled by default, but it is possible to disable it. Other required extensions include PCRE and SPL. These standard extensions cannot be disabled without patching PHP’s build system and/or C sources.
ramsey/uuid recommends installing/enabling the following extensions. While not required, these extensions improve the performance of ramsey/uuid.
Install With Composer¶
The only supported installation method for ramsey/uuid is Composer. Use the following command to add ramsey/uuid to your project dependencies:
composer require ramsey/uuid
Using ramsey/uuid¶
After installing ramsey/uuid, the quickest way to get up-and-running is to use the static generation methods.
use Ramsey\Uuid\Uuid;
$uuid = Uuid::uuid4();
printf(
"UUID: %s\nVersion: %d\n",
$uuid->toString(),
$uuid->getFields()->getVersion()
);
This will return an instance of Ramsey\Uuid\Rfc4122\UuidV4
.
Tip
Use the Interfaces
Feel free to use instanceof
to check the specific instance types of
UUIDs. However, when using type hints, it’s best to use the interfaces.
The most lenient interface is Ramsey\Uuid\UuidInterface
,
while Ramsey\Uuid\Rfc4122\UuidInterface
ensures the
UUIDs you’re using conform to the RFC 4122 standard. If you’re not sure
which one to use, start with the stricter
Rfc4122\UuidInterface
.
ramsey/uuid provides a number of helpful static methods that help you work with and generate most types of UUIDs, without any special customization of the library.
Method |
Description |
---|---|
This generates a Version 1: Gregorian Time UUID. |
|
This generates a Version 2: DCE Security UUID. |
|
This generates a Version 3: Name-based (MD5) UUID. |
|
This generates a Version 4: Random UUID. |
|
This generates a Version 5: Name-based (SHA-1) UUID. |
|
This generates a Version 6: Reordered Time UUID. |
|
This generates a Version 7: Unix Epoch Time UUID. |
|
Checks whether a string is a valid UUID. |
|
Creates a UUID instance from a string UUID. |
|
Creates a UUID instance from a 16-byte string. |
|
Creates a UUID instance from a string integer. |
|
Creates a version 1 UUID instance from a PHP DateTimeInterface. |