UUID 是 通用唯一識別碼(Universally Unique Identifier)的縮寫,是一種軟件建構(gòu)的標準,亦為開放軟件基金會組織在分布式計算環(huán)境領(lǐng)域的一部分。其目的,是讓分布式系統(tǒng)中的所有元素,都能有唯一的辨識信息,而不需要通過中央控制端來做辨識信息的指定。
UUID由以下幾部分的組合:
(1)當前日期和時間,UUID的第一個部分與時間有關(guān),如果你在生成一個UUID之后,過幾秒又生成一個UUID,則第一個部分不同,其余相同。
(2)時鐘序列。
(3)全局唯一的IEEE機器識別號,如果有網(wǎng)卡,從網(wǎng)卡MAC地址獲得,沒有網(wǎng)卡以其他方式獲得。(來自百度百科)
1. 利用mysql uuid()函數(shù)
$row= Yii::$app->db->createCommand("select uuid() as uuid")->queryOne();
echo $row['uuid'];
執(zhí)行結(jié)果(5次)
b8cc7eca-125e-11e9-8b0d-080027b68021
b8cc873d-125e-11e9-8b0d-080027b68021
b8cc8e51-125e-11e9-8b0d-080027b68021
b8cc94a4-125e-11e9-8b0d-080027b68021
b8cc9ace-125e-11e9-8b0d-080027b68021
2. 使用Faker\Provider\Uuid;這個類庫(偽uuid)
use Faker\Provider\Uuid;
echo Uuid::uuid();
執(zhí)行結(jié)果(5次)
866b6cd0-4f64-3427-8f69-36fd46f7f497
148aac48-73f8-3522-b2c2-15b04942b016
668a65aa-fffb-361b-875c-6a7ec7eebfcb
a9331537-48b2-3387-9879-649f20988982
3d6bb628-1eff-34cb-882f-72a519373975
這個類的源碼
public static function uuid()
{
// fix for compatibility with 32bit architecture; seed range restricted to 62bit
$seed = mt_rand(0, 2147483647) . '#' . mt_rand(0, 2147483647);
// Hash the seed and convert to a byte array
$val = md5($seed, true);
$byte = array_values(unpack('C16', $val));
// extract fields from byte array
$tLo = ($byte[0] << 24) | ($byte[1] << 16) | ($byte[2] << 8) | $byte[3];
$tMi = ($byte[4] << 8) | $byte[5];
$tHi = ($byte[6] << 8) | $byte[7];
$csLo = $byte[9];
$csHi = $byte[8] & 0x3f | (1 << 7);
// correct byte order for big edian architecture
if (pack('L', 0x6162797A) == pack('N', 0x6162797A)) {
$tLo = (($tLo & 0x000000ff) << 24) | (($tLo & 0x0000ff00) << 8)
| (($tLo & 0x00ff0000) >> 8) | (($tLo & 0xff000000) >> 24);
$tMi = (($tMi & 0x00ff) << 8) | (($tMi & 0xff00) >> 8);
$tHi = (($tHi & 0x00ff) << 8) | (($tHi & 0xff00) >> 8);
}
// apply version number
$tHi &= 0x0fff;
$tHi |= (3 << 12);
// cast to string
$uuid = sprintf(
'%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x',
$tLo,
$tMi,
$tHi,
$csHi,
$csLo,
$byte[10],
$byte[11],
$byte[12],
$byte[13],
$byte[14],
$byte[15]
);
return $uuid;
}