关键在于create_uuid和create_guid方法
<?php
class GetUUID
{
private $uuid;
private static $instance;
public static function getInstance()
{
if (!(self::$instance instanceof self)) {
self::$instance = new self();
}
return self::$instance;
}
public function get($type = 'uuid', $prefix = '')
{
$action = 'create_' . $type;
self::$instance->$action();
return $prefix . $this->uuid;
}
private function create_uuid()
{
$chars = md5(uniqid(mt_rand(), true));
$hyphen = chr(45);
$this->uuid = substr($chars, 0, 8)
. $hyphen
. substr($chars, 8, 4)
. $hyphen
. substr($chars, 12, 4)
. $hyphen
. substr($chars, 16, 4)
. $hyphen
. substr($chars, 20, 12);
}
private function create_guid()
{
$charid = strtoupper(md5(uniqid(mt_rand(), true)));
$hyphen = chr(45);
$this->uuid = substr($charid, 6, 2)
. substr($charid, 4, 2)
. substr($charid, 2, 2)
. substr($charid, 0, 2)
. $hyphen
. substr($charid, 10, 2)
. substr($charid, 8, 2)
. $hyphen
. substr($charid,14, 2)
. substr($charid,12, 2)
. $hyphen
. substr($charid,16, 4)
. $hyphen
. substr($charid,20,12);
}
private function __construct() {}
private function __clone() {}
}
echo GetUUID::getInstance()->get();
echo "\n";
echo GetUUID::getInstance()->get('guid');
echo "\n";
echo GetUUID::getInstance()->get('uuid', 'aaaa');