Moodle PHP Documentation 4.2
Moodle 4.2.8 (Build: 20240610) (2d41ac46f45)
core\uuid Class Reference

V4 UUID generator class. More...

Static Public Member Functions

static generate ()
 Generate a V4 UUID.
 

Static Protected Member Functions

static generate_uuid_via_pecl_uuid_extension ()
 Generate a V4 UUID using PECL UUID extension.
 
static generate_uuid_via_random_bytes ()
 Generate a V4 UUID using PHP 7+ features.
 

Detailed Description

V4 UUID generator class.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

Member Function Documentation

◆ generate()

static core\uuid::generate ( )
static

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.

See also
https://tools.ietf.org/html/rfc4122
Return values
stringThe V4 UUID.

◆ generate_uuid_via_pecl_uuid_extension()

static core\uuid::generate_uuid_via_pecl_uuid_extension ( )
staticprotected

Generate a V4 UUID using PECL UUID extension.

See also
https://github.com/php/pecl-networking-uuid PECL uuid
https://tools.ietf.org/html/rfc4122
Return values
string|boolThe UUID when PECL UUID extension is available; otherwise, false.

◆ generate_uuid_via_random_bytes()

static core\uuid::generate_uuid_via_random_bytes ( )
staticprotected

Generate a V4 UUID using PHP 7+ features.

See also
https://www.php.net/manual/en/function.random-bytes.php
https://tools.ietf.org/html/rfc4122
Return values
string|boolThe UUID when random_bytes() function is available; otherwise, false when missing the sources of randomness used by random_bytes().

The documentation for this class was generated from the following file: