PHP code example of teracrafts / flagkit

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

    

teracrafts / flagkit example snippets




use FlagKit\FlagKit;
use FlagKit\FlagKitOptions;

// Initialize the SDK
$options = new FlagKitOptions(apiKey: 'sdk_your_api_key');
$client = FlagKit::initializeAndStart($options);

// Identify user
FlagKit::identify('user-123', [
    'plan' => 'premium',
    'beta' => true,
]);

// Evaluate flags
$darkMode = FlagKit::getBooleanValue('dark-mode', defaultValue: false);
$theme = FlagKit::getStringValue('theme', defaultValue: 'light');
$maxItems = FlagKit::getIntValue('max-items', defaultValue: 10);

// Track events
FlagKit::track('button_clicked', ['button' => 'signup']);

// Cleanup when done
FlagKit::close();



use FlagKit\FlagKitOptions;

$options = FlagKitOptions::builder('sdk_your_api_key')
    ->pollingInterval(60)
    ->cacheTtl(600)
    ->maxCacheSize(500)
    ->cacheEnabled(true)
    ->eventBatchSize(20)
    ->eventFlushInterval(60)
    ->eventsEnabled(true)
    ->timeout(30)
    ->retryAttempts(5)
    ->build();

$client = FlagKit::initialize($options);
$client->initialize();

$options = new FlagKitOptions(
    apiKey: 'sdk_your_api_key',
    pollingInterval: 60,
    cacheTtl: 600,
    maxCacheSize: 500,
);



use FlagKit\EvaluationContext;
use FlagKit\FlagKit;

// Using builder pattern
$context = EvaluationContext::builder()
    ->userId('user-123')
    ->attribute('plan', 'premium')
    ->attribute('beta', true)
    ->attribute('score', 95.5)
    ->build();

$result = FlagKit::evaluate('feature-flag', $context);

// Using fluent methods
$context = (new EvaluationContext())
    ->withUserId('user-123')
    ->withAttribute('plan', 'premium')
    ->withAttributes([
        'region' => 'us-east',
        'beta' => true,
    ]);

// Boolean flags
$enabled = FlagKit::getBooleanValue('feature-enabled', defaultValue: false);

// String flags
$variant = FlagKit::getStringValue('experiment-variant', defaultValue: 'control');

// Number flags
$limit = FlagKit::getNumberValue('rate-limit', defaultValue: 100.0);
$count = FlagKit::getIntValue('max-count', defaultValue: 10);

// JSON flags
$config = FlagKit::getJsonValue('feature-config', defaultValue: null);

$result = FlagKit::evaluate('feature-flag');

echo "Flag: " . $result->flagKey . "\n";
echo "Value: " . print_r($result->value->getRaw(), true) . "\n";
echo "Enabled: " . ($result->enabled ? 'true' : 'false') . "\n";
echo "Reason: " . $result->reason->value . "\n";
echo "Version: " . $result->version . "\n";

// Identify user with attributes
FlagKit::identify('user-123', [
    'email' => '[email protected]',
    'plan' => 'enterprise',
    'created_at' => date('c'),
]);

// Update context
FlagKit::setContext(
    (new EvaluationContext())
        ->withUserId('user-456')
        ->withAttribute('admin', true)
);

// Clear context
FlagKit::clearContext();

// Track custom events
FlagKit::track('purchase_completed', [
    'amount' => 99.99,
    'currency' => 'USD',
    'product_id' => 'prod-123',
]);

// Flush pending events
FlagKit::flush();

$options = FlagKitOptions::builder('sdk_your_api_key')
    ->bootstrap([
        'dark-mode' => true,
        'theme' => 'dark',
        'max-items' => 50,
    ])
    ->build();

$client = FlagKit::initialize($options);
// Flags available immediately from bootstrap



use FlagKit\FlagKitException;
use FlagKit\ErrorCode;

try {
    $client->initialize();
} catch (FlagKitException $e) {
    if ($e->isConfigError()) {
        echo "Configuration error: " . $e->getMessage() . "\n";
    } elseif ($e->isNetworkError()) {
        echo "Network error: " . $e->getMessage() . "\n";
    } else {
        echo "Error [{$e->getErrorCode()->value}]: " . $e->getMessage() . "\n";
    }
}

$options = FlagKitOptions::builder('sdk_your_api_key')
    ->build();

$client = FlagKit::initialize($options);