Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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.