PHP code example of joby / smol-uid

1. Go to this page and download the library: Download joby/smol-uid library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

joby / smol-uid example snippets


use Joby\Smol\UID\UID;

// Generate a new UID
// Default is version 0, which is fully random
// Version 1.1 keeps the full timestamp
// Versions 1.2-1.4 trim increasing amounts of precision from the timestamp
$uid = UID::generate(UID::VERSION_1_1);

use Joby\Smol\UID\UID;

// Generate a new UID
// in this case a fully random one
$uid = UID::generate();

// Can be used as or cast to a string
// will be a 10-13 character alphanumeric string
$string = (string) $uid;

// can be turned back into a UID object
$sameUID = UID::fromString($string);

use Joby\Smol\UID\UID;

// Create a UID
$uid = UID::generate();

// Get the underlying integer
$int = $uid->value;

// Convert back to a UID
$sameUID = UID::fromInt($int);

use Joby\Smol\UID\UID;

// Create a UID and a copy from its string
$uid1 = UID::generate();
$uid2 = UID::fromString((string) $uid1);

// They have the same integer value, so == works
$uid1 == $uid2; // true

// The library also ensures they are the same object, so === works too
$uid1 === $uid2; // true

use Joby\Smol\UID\UID;

$uid = UID::generate();

// Get the version bits
$version = $uid->version();

// Get the approximate timestamp when this UID was created
// This returns the lower bound of when this UID could have been created
$timestamp = $uid->time();

// Get the random bits of the UID
$random = $uid->random();

// Store as a string (more readable)
$db->query("INSERT INTO users (id, name) VALUES (?, ?)", [(string)$uid, "John"]);

// Store as an integer (more efficient)
$db->query("INSERT INTO users (id, name) VALUES (?, ?)", [$uid->value, "John"]);

use Joby\Smol\UID\UID;

// derive from a simple SHA256 hash
// faster and easier, but underlying values are less protected
$uid = UID::hashGenerate('some value to generate from');

// derive from an HMAC hash with secret key
// makes guessing underlying values much harder
$uid = UID::hmacGenerate('some value to generate from', 'secret key');