PHP code example of signakit / flags-php

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

    

signakit / flags-php example snippets




ignaKit\FlagsPhp\SignaKitClient;

$client = new SignaKitClient('sk_prod_abc123_1234_xxxxxxxxxxxx');
$client->initialize(); // fetches config from CDN, throws on failure

$ctx      = $client->createUserContext('user-123', ['plan' => 'premium', 'country' => 'US']);
$decision = $ctx->decide('new-checkout');

if ($decision?->enabled && $decision->variationKey === 'treatment') {
    // show new checkout
} else {
    // show control
}

// Track a conversion
$ctx->trackEvent('purchase', value: 99.99);

// AppServiceProvider::register()
$this->app->singleton(SignaKitClient::class, function () {
    $client = new SignaKitClient(config('services.signakit.sdk_key'));
    $client->initialize();
    return $client;
});

// In a controller / middleware
public function show(Request $request, SignaKitClient $flags): Response
{
    $ctx      = $flags->createUserContext($request->user()->id, [
        'plan'    => $request->user()->plan,
        'country' => $request->header('CF-IPCountry', 'US'),
    ]);
    $decision = $ctx->decide('new-checkout');

    return view('checkout', ['variant' => $decision?->variationKey ?? 'control']);
}

// functions.php or a plugin bootstrap file
add_action('init', function () {
    $client = new \SignaKit\FlagsPhp\SignaKitClient(defined('SIGNAKIT_SDK_KEY') ? SIGNAKIT_SDK_KEY : '');
    $client->initialize();
    $GLOBALS['signakit'] = $client;
});

// In a template
$ctx      = $GLOBALS['signakit']->createUserContext(get_current_user_id(), ['plan' => 'free']);
$decision = $ctx->decide('new-header');

if ($decision?->variationKey === 'treatment') {
    get_template_part('partials/header-new');
} else {
    get_template_part('partials/header');
}

new SignaKitClient(string $sdkKey, ?HttpClientInterface $httpClient = null)

$ctx = $client->createUserContext('user-123', ['plan' => 'premium']);

readonly class Decision {
    public string  $flagKey;
    public string  $variationKey; // 'off' when flag is stopped
    public bool    $enabled;      // false only when flag is stopped
    public ?string $ruleKey;      // null when default allocation was used
}

use SignaKit\FlagsPhp\Contracts\HttpClientInterface;

final class MyHttpClient implements HttpClientInterface
{
    public function get(string $url, array $headers = []): array { /* ... */ }
    public function post(string $url, array $headers = [], string $body = ''): void { /* ... */ }
}

$client = new SignaKitClient('sk_prod_...', httpClient: new MyHttpClient());
bash
composer