1. Go to this page and download the library: Download erikwang2013/snowflake-php 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/ */
erikwang2013 / snowflake-php example snippets
use Snowflake\Snowflake;
$snowflake = new Snowflake();
$id = $snowflake->id(); // e.g. 508047278033704960
$id = $snowflake->nextId(); // alias for id()
$snowflake = new Snowflake(workerId: 5, datacenterId: 3);
$id = $snowflake->id();
// Container
$id = app('snowflake')->id();
// Dependency injection
use Snowflake\Snowflake;
class IndexController
{
public function index(Snowflake $snowflake)
{
$id = $snowflake->id();
}
}
// Facade
use Snowflake\Adapters\ThinkPHP\Facade;
$id = Facade::id();
use Snowflake\Snowflake;
return [
Snowflake::class => function () {
return Snowflake::fromConfig(config('snowflake'));
},
];
use Snowflake\Snowflake;
class OrderService
{
public function __construct(private Snowflake $snowflake) {}
public function create(): int
{
return $this->snowflake->id();
}
}