Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

V4 UUID generator.

Copyright: 2019 Matteo Scaramuccia <moodle@matteoscaramuccia.com>
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 144 lines (5 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: uuid  - X-Ref

V4 UUID generator class.

generate_uuid_via_pecl_uuid_extension()   X-Ref
Generate a V4 UUID using PECL UUID extension.

return: string|bool The UUID when PECL UUID extension is available;

generate_uuid_via_random_bytes()   X-Ref
Generate a V4 UUID using PHP 7+ features.

return: string|bool The UUID when random_bytes() function is available;

generate()   X-Ref
Generate a V4 UUID.

Unique is hard. Very hard. Attempt to use the PECL UUID function if available, and if not then revert to
constructing the uuid using random_bytes or mt_rand.

It is important that this token is not solely based on time as this could lead
to duplicates in a clustered environment (especially on VMs due to poor time precision).

UUIDs are just 128 bits long but with different supported versions (RFC 4122), mainly two:
- V1: the goal is uniqueness, at the cost of anonymity since it is coupled to the host generating it, via its MAC address.
- V4: the goal is randomness, at the cost of (rare) collisions.
Here, the V4 type is the preferred choice.

The format is:
xxxxxxxx-xxxx-4xxx-Yxxx-xxxxxxxxxxxx
where x is any hexadecimal digit and Y is a random choice from 8, 9, a, or b.

return: string The V4 UUID.