Programster's Blog

Tutorials focusing on Linux, programming, and open-source

PHP Lecture - Identify All the Things With UUIDs!

After hearing about UUIDs from a talk on Doctrine ORM good practices, I wanted to learn a little bit more about them before starting to use them in my databases. Below is a lightning talk from the author/maintainer of ramsey/uuid, the PHP package library recommended in the Doctrine lecture for generating the UUIDs.



Hopefully you found that video helpful. The main points I took from it are that:

  • Versions 1,2, and 4 generate new unique UUIDs from random bytes, but versions 3 and 5 will always generate the same UUID based on the inputs given to them.
  • Versions 3 and 5 are the same, except 5 uses a different hashing algorithm, SHA-1 instead of md5
  • Version 4 can be made to be sequential, making it more suitable for databases so that later records are always appended, rather than injected randomly throughout the table.


Below is my tweaked version of the example code that generated sequentialy UUID v4.


 * In order to be able to run this script, you need to use the following
 * commands in order to install the necessary packages:
 *  composer require ramsey/uuid
 *  composer require moontoast/math 

require_once(__DIR__ . '/vendor/autoload.php');

$factory = new \Ramsey\Uuid\UuidFactory();

$generator = new \Ramsey\Uuid\Generator\CombGenerator(

$codec = new Ramsey\Uuid\Codec\TimestampFirstCombCodec($factory->getUuidBuilder());



for ($i=0; $i<5; $i++)
    echo Ramsey\Uuid\Uuid::uuid4()->toString() . PHP_EOL;