A UUID (Universally unique identifier) is a 128-bit number used to identify information in computer systems. They are becoming increasingly popular in PHP/Symfony applications, where some developers use them even as IDs for database records (instead of the traditional auto increment integer).
There are different variants and versions of UUIDs, so most developers rely on external libraries such as the UUID PECL package and the ramsey/uuid library to generate these UUIDs.
Symfony Polyfills provide PHP reimplementations of popular functions, classes
and constants introduced in newer PHP versions or PHP extensions. They allow for
example to use the PHP 7.3 JsonException
class in any PHP version or the
PHP 7.4 password_algos()
function in any previous PHP version.
Given the popularity of UUIDs, we've created a new UUID polyfill that reimplements the UUID PECL package and can be used in PHP 5.3 or higher. First, install the polyfill in your project:
1
$ composer require symfony/polyfill-uuid
If the UUID PECL package is installed in your system, this polyfill does nothing. Otherwise, it defines the exact same constants and functions of the PECL package so you can use the following in your code:
1 2 3 4 5 6 7 8
$uuid = uuid_create(UUID_TYPE_RANDOM);
// $uuid = '79a0f84a-2f15-4ea9-bb2c-49e645845100'
$isValid = uuid_is_valid($uuid);
// $isValid = true
// Other useful functions: uuid_compare(), uuid_is_null(), uuid_type(),
// uuid_variant(), uuid_time(), uuid_mac(), uuid_parse(), uuid_unparse()
The UUID Polyfill can replace the UUID PECL package entirely when you can't or
don't want to install the PECL package. However, it doesn't provide (and it'll
never provide) the same features of full-featured libraries such as ramsey/uuid
.
Regarding performance, the polyfill is faster or slower than the PECL package depending on the UUID version being created:
Time to create UUID v1 | Mean | Difference |
---|---|---|
PECL | 0.525?s | Fastest |
Symfony Polyfill | 1.401?s | 2.67x slower |
Ramsey/UUID | 3.907?s | 7.45x slower |
Time to create UUID v4 | Mean | Difference |
---|---|---|
PECL | 4.620?s | 3.25x slower |
Symfony Polyfill | 1.422?s | Fastest |
Ramsey/UUID | 1.851?s | 1.30x slower |
If you want to get more information about the numbers, you can read the following article https://jolicode.com/blog/uuid-generation-in-php
Thanks, this looks great. What is the recommended way to use UUID's as a primary key in Doctrine? https://github.com/ramsey/uuid-doctrine is obviously one way for ramsey uuid's, will there be a Symfony way as well? I think MySQL and PostGRES have native UUID types, but SQLite does not. So how should this field be declared as a field (or primary key) in an entity?
Also, lots of warnings: https://github.com/symfony/symfony/issues/34712
@Tac Tacelosky it might make sense to leave primary keys as integers in SQLite and just add another field for UUID, index it and use that when fetching stuff. The reason being that joins are much slower on VARCHARs than on INTs. Titouan Galopin wrote a nice article about it: https://titouangalopin.com/auto-increment-is-the-devil-using-uuids-in-symfony-and-doctrine/