PHP code example of ez-php / feature-flags

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

    

ez-php / feature-flags example snippets


\EzPhp\FeatureFlags\FeatureFlagServiceProvider::class,

use EzPhp\FeatureFlags\Flag;

if (Flag::enabled('new-checkout')) {
    // show new checkout flow
}

if (Flag::disabled('dark-mode')) {
    // show light theme
}

// Context-aware evaluation (e.g. per-user gradual rollouts)
if (Flag::enabledFor('beta-search', $user->id)) {
    // show beta search to this user
}

if (Flag::disabledFor('new-ui', $user->id)) {
    // show legacy UI for this user
}

$all = Flag::all(); // ['new-checkout' => true, 'dark-mode' => false]

return [
    'driver' => getenv('FLAGS_DRIVER') ?: 'file',  // file | database | redis | array
    'file'   => getenv('FLAGS_FILE') ?: 'flags.php',
    'redis'  => [
        'host'     => getenv('FLAGS_REDIS_HOST') ?: '127.0.0.1',
        'port'     => (int) (getenv('FLAGS_REDIS_PORT') ?: 6379),
        'database' => (int) (getenv('FLAGS_REDIS_DATABASE') ?: 0),
    ],
];



return [
    'new-checkout' => true,
    'dark-mode'    => false,
    'beta-search'  => false,
];

$pdo->exec('
    CREATE TABLE feature_flags (
        name    VARCHAR(255) NOT NULL PRIMARY KEY,
        enabled TINYINT(1)   NOT NULL DEFAULT 0
    )
');

$pdo->exec('
    CREATE TABLE feature_flag_contexts (
        name       VARCHAR(255) NOT NULL,
        context_id VARCHAR(255) NOT NULL,
        enabled    TINYINT(1)   NOT NULL DEFAULT 0,
        PRIMARY KEY (name, context_id)
    )
');

$redis->hSet('feature_flags', 'new-checkout', '1');
$redis->hSet('feature_flags:contexts:beta-search', '42', '1'); // per-context override

return [
    'driver' => 'file',
    'file'   => 'flags.php',
    'rollouts' => [
        'new-checkout' => 25,   // 25 % of contexts
    ],
];

Flag::enabledFor('new-checkout', $user->id);   // same user → same answer, every time
bash
composer