PHP code example of boondoc / uuid

1. Go to this page and download the library: Download boondoc/uuid 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/ */

    

boondoc / uuid example snippets


use Boondoc\uuid;

$u = new uuid ([ mixed $uuid = '' [, mixed $namespace = null ] ] );

$u = new uuid ('fae0b286-007f-3824-a616-16d7332b7a09'); // Imports the specified string

$u1 = new uuid ('fae0b286-007f-3824-a616-16d7332b7a09');
$u2 = new uuid ($u1); // Imports the value of $u1 – essentially $u2 is now a clone of $u1

$u = new uuid (); // Generates a new UUID v4

$u1 = new uuid ('String to be hashed', '87e1d1be-df8d-11e7-b804-0800276fa7d4'); // Generates a new UUID v5 using '87e1…' as the namespace
$u2 = new uuid ('String to be hashed', $u1); // Generates a new UUID v5 using $u1 as the namespace

echo new uuid (uuid::getDefaultNamespace ()); // '00000000-0000-0000-0000-000000000000', aka the “nil” UUID

$u1 = new uuid ('String to be hashed'); // Generates a new UUID v5 using '0000…' as the namespace

uuid::setDefaultNamespace ('87e1d1be-df8d-11e7-b804-0800276fa7d4');

$u2 = new uuid ('String to be hashed'); // Generates a new UUID v5 using '87e1…' as the namespace

$u = new uuid ('', false); // Generates a new UUID v4

try
{
	$u = new uuid ('String to be hashed', false);
}
catch (InvalidString $e)
{
	echo $e->getMessage () . PHP_EOL;
}

$u1 = uuid::v4 (); // Generates a new UUID v4
$u2 = uuid::v5 ('String to be hashed'); // Generates a new UUID v5 using the default namespace
$u3 = uuid::v5 ('String to be hashed', '87e1d1be-df8d-11e7-b804-0800276fa7d4'); // Generates a new UUID v5 using '87e1…' as the namespace

$u1 = uuid::v1 (); // Generates a new UUID v1
$u2 = uuid::v3 ('String to be hashed'); // Generates a new UUID v3 using the default namespace
$u3 = uuid::v3 ('String to be hashed', '87e1d1be-df8d-11e7-b804-0800276fa7d4'); // Generates a new UUID v3 using '87e1…' as the namespace

echo bin2hex (uuid::getMACAddress ()); // '000000000000'

uuid::setMACAddress ('01:23:45:67:89:AB');

echo $u->long			. PHP_EOL;	// fae0b286-007f-3824-a616-16d7332b7a09
echo $u->short			. PHP_EOL;	// -uCyhgB_OCSmFhbXMyt6CQ
echo bin2hex ($u->binary)	. PHP_EOL;	// fae0b286007f3824a61616d7332b7a09
echo $u				. PHP_EOL;	// fae0b286-007f-3824-a616-16d7332b7a09

echo $u				. PHP_EOL;	// fae0b286-007f-3824-a616-16d7332b7a09
echo $u->version		. PHP_EOL;	// 3
echo $u->variant		. PHP_EOL;	// 1


	Boondoc\uuid;

	uuid::setDefaultNamespace ('0f1abe4a-df91-11e7-86fc-0800276fa7d4'); // See Note 1

	// Import; these all produce objects with identical internal values
	$uuid_long			= new uuid ('fae0b286-007f-3824-a616-16d7332b7a09');
	$uuid_caps			= new uuid ('FAE0B286-007F-3824-A616-16D7332B7A09');
	$uuid_guid			= new uuid ('{fae0b286-007f-3824-a616-16d7332b7a09}');
	$uuid_short			= new uuid ('-uCyhgB_OCSmFhbXMyt6CQ');
	$uuid_base64			= new uuid ('+uCyhgB/OCSmFhbXMyt6CQ==');
	$uuid_binary			= new uuid ("\xFA\xE0\xB2\x86\x00\x7F\x38\x24\xA6\x16\x16\xD7\x33\x2B\x7A\x09");
	$uuid_suppress_namespace	= new uuid ('fae0b286-007f-3824-a616-16d7332b7a09', false);

	// Instance generation
	$uuid_v4			= new uuid ();
	$uuid_v5_global_namespace	= new uuid ('String to be hashed'); // Namespace is '0f1a…'
	$uuid_v5_string_namespace	= new uuid ('String to be hashed', '87e1d1be-df8d-11e7-b804-0800276fa7d4');
	$uuid_v5_object_namespace	= new uuid ('String to be hashed', $uuid_v4);
	$uuid_v5_constant_namespace	= new uuid ('String to be hashed',  UUID_NAMESPACE_OID); // See Note 2

	// Factory generation
	$uuid_v1			= uuid::v1 ();
	$uuid_v3_global_namespace	= uuid::v3 ('String to be hashed'); // Namespace is '0f1a…'
	$uuid_v3_string_namespace	= uuid::v3 ('String to be hashed', '87e1d1be-df8d-11e7-b804-0800276fa7d4');
	$uuid_v3_object_namespace	= uuid::v3 ('String to be hashed', $uuid_v1);
	$uuid_v3_constant_namespace	= uuid::v3 ('String to be hashed',  UUID_NAMESPACE_OID); // See Note 2

	$uuid_v4			= uuid::v4 ();
	$uuid_v5_global_namespace	= uuid::v5 ('String to be hashed'); // Namespace is '0f1a…'
	$uuid_v5_string_namespace	= uuid::v5 ('String to be hashed', '87e1d1be-df8d-11e7-b804-0800276fa7d4');
	$uuid_v5_object_namespace	= uuid::v5 ('String to be hashed', $uuid_v4);
	$uuid_v5_constant_namespace	= uuid::v5 ('String to be hashed',  UUID_NAMESPACE_OID); // See Note 2

	// Output formats
	echo $uuid_long->long				. PHP_EOL;	// fae0b286-007f-3824-a616-16d7332b7a09
	echo $uuid_long->short				. PHP_EOL;	// -uCyhgB_OCSmFhbXMyt6CQ
	echo bin2hex ($uuid_long->binary)		. PHP_EOL;	// fae0b286007f3824a61616d7332b7a09
	echo $uuid_long 				. PHP_EOL;	// fae0b286-007f-3824-a616-16d7332b7a09

	// Validation
	uuid::isValidLong ('fae0b286-007f-3824-a616-16d7332b7a09');	// true
	uuid::isValidLong ('00000000-0000-0000-0000-000000000000');	// true: nil UUID
	uuid::isValidLong ('FAE0B286-007F-3824-A616-16D7332B7A09');	// true: uppercase is permitted on input
	uuid::isValidLong ('{fae0b286007f3824a61616d7332b7a09}');	// true: dashes and end-braces are both optional
	uuid::isValidLong ('fae0b286-007f-e824-a616-16d7332b7a09');	// false: there is no UUID version 'E'
	uuid::isValidLong ('fae0b286-007f-3824-c616-16d7332b7a09');	// false: variant 2 is not permitted (character 'c')
	uuid::isValidLong ('fae0b286-007f-j824-a616-16d7332b7a09');	// false: character 'j' is not permitted
	uuid::isValidLong ('fae0b286-007f-3824-a61616d7-332b7a09');	// false: dash in the wrong place
	uuid::isValidLong ('fae0b286-007f-3824-a616-16d7332b7a0936');	// false: wrong length

	uuid::isValidShort ('-uCyhgB_OCSmFhbXMyt6CQ');			// true
	uuid::isValidShort ('AAAAAAAAAAAAAAAAAAAAAA');			// true: nil UUID
	uuid::isValidShort ('+uCyhgB/OCSmFhbXMyt6CQ==');		// true: standard base64 charset is permitted on input, padding is optional
	uuid::isValidShort ('-uCyhgB_OCSmFhbXMyt6Cj');			// false: final character must be one of [g, w, A, Q]
	uuid::isValidShort ('-uCyhgB_OCSmFhbXMyt6CQm'); 		// false: wrong length

	uuid::isValidBinary ("\xFA\xE0\xB2\x86\x00\x7F\x38\x24\xA6\x16\x16\xD7\x33\x2B\x7A\x09");	// true
	uuid::isValidBinary ("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");	// true: nil UUID
	uuid::isValidBinary ("\xFA\xE0\xB2\x86\x00\x7F\xE8\x24\xA6\x16\x16\xD7\x33\x2B\x7A\x09");	// false: there is no UUID version 'E'
	uuid::isValidBinary ("\xFA\xE0\xB2\x86\x00\x7F\xE8\x24\xA6\x16\x16\xD7\x33\x2B\x7A\x09\x36");	// false: wrong length

	uuid::isValid ('fae0b286-007f-3824-a616-16d7332b7a09');		// true		// See Note 3
	uuid::isValid ($uuid_v4);					// true