PHP code example of c0dec0de / snowflake-id

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

    

c0dec0de / snowflake-id example snippets




declare(strict_types=1);

e;

$snowflakeIDIntType = Snowflake::int();

var_dump($snowflakeIDIntType); // e.g., int(195302773577621504)



declare(strict_types=1);

e;

$snowflakeIDStringType = Snowflake::string();

var_dump($snowflakeIDStringType); // e.g., string(195302773577621504)



declare(strict_types=1);

\Store\RedisStore;
use C0dec0de\Snowflake\Sequence\Store\StoreInterface;
use C0dec0de\Snowflake\Snowflake;

/**
 * Instantiate and configure your preferred Redis client (phpredis or predis).
 */
$client = new Redis();
$client->connect(
    host: 'keyval',
    port: 6379,
    timeout: 2.5,
);

/**
 * Pass the Redis client instance to RedisStore.
 */
$redisStore = new RedisStore($client);

/**
 * Generate a Snowflake ID with:
 * 
 * @param StoreInterface|null $store        - Pass the configured RedisStore instance; if null, InMemoryStore is used.
 * @param int|null            $datacenterId - Unique datacenter ID (configure per datacenter).
 * @param int|null            $workerId     - Unique worker/machine ID within the datacenter.
 */
$snowflakeIDIntType = Snowflake::int(
    store: $redisStore,
    datacenterId: 1,
    workerId: 1,
);

var_dump($snowflakeIDIntType); // e.g., int(195302773577621504)