php ライブラリ「ramsey/uuid」を使ってUUIDを生成する

phpでライブラリ「ramsey/uuid」を使ってUUIDを生成するまでのサンプルコードを記述してます。composerを使用して「ramsey/uuid」はインストールしてます。
環境
- OS windows10 pro 64bit
- Apache 2.4.43
- PHP 7.4.5
- Composer 1.10.5
※windows10にApacheのインストールはこちら
※windows10にphpのインストールはこちら
※Windows10にComposerのインストールはこちら
ramsey/uuidインストール
phpが動作しているフォルダで、composerを使用してインストールします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
composer require ramsey/uuid <出力結果> Using version ^4.0 for ramsey/uuid ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 3 installs, 0 updates, 0 removals - Installing ramsey/collection (1.0.1): Loading from cache - Installing brick/math (0.8.15): Loading from cache - Installing ramsey/uuid (4.0.1): Loading from cache ramsey/uuid suggests installing ext-gmp (Enables faster math with arbitrary-precision integers using GMP.) ramsey/uuid suggests installing ext-uuid (Enables the use of PeclUuidTimeGenerator and PeclUuidRandomGenerator.) ramsey/uuid suggests installing ramsey/uuid-doctrine (Allows the use of Ramsey\Uuid\Uuid as Doctrine field type.) ramsey/uuid suggests installing paragonie/random-lib (Provides RandomLib for use with the RandomLibAdapter) Package mschop/noteephp is abandoned, you should avoid using it. Use mschop/notee instead. Writing lock file Generating autoload files 23 packages you are using are looking for funding. Use the `composer fund` command to find out more! |
自分の場合は、下記にインストールしました。

ramsey/uuid利用
「C:\Apache24\htdocs」に「test.php」を下記のコードで作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php ini_set('display_errors', "On"); require_once __DIR__ . '/vendor/autoload.php'; use Ramsey\Uuid\Uuid; // Timestanpから生成 echo Uuid::uuid1()."<br>"; // MD5から生成 echo Uuid::uuid3(Uuid::NAMESPACE_DNS, 'keywokokoni')."<br>"; // ランダムで生成 echo Uuid::uuid4(); |
実行結果を確認すると、UUIDが生成されていることが確認できます。

-
前の記事
jquery closestメソッドを使って指定した要素以外のクリックイベントを判別する 2020.08.16
-
次の記事
javascript document.writeで生成したテキストにリンクをつける 2020.08.17
コメントを書く