PHP code example of xepozz / feature-flag

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

    

xepozz / feature-flag example snippets


\Xepozz\FeatureFlag\Driver\InMemoryDriver::class => [
    '__construct()' => [
        'flags' => [
            155 => false,
            'feature_name' => true,
            FeaturesEnum::FEATURE_NAME => true,
        ],
    ],
],

'xepozz/feature-flag' => [
    'flags' => [
        155 => false,
        'feature_name' => true,
        FeaturesEnum::FEATURE_NAME => true,
    ],
],

\Xepozz\FeatureFlag\Driver\RedisDriver::class => function () {
    $redis = new Redis();
    $redis->pconnect(
        host: '127.0.0.1',
        port: 6379,
        timeout: 2.5,
    );

    return new \Xepozz\FeatureFlag\Driver\RedisDriver(redis: $redis, hashTableKey: 'ab');
},

use Xepozz\FeatureFlag\FlagStorageInterface;
use \Xepozz\FeatureFlag\Driver\RedisDriver;

return [
    // ...
    FlagStorageInterface::class => RedisDriver::class,
    // ...
]

use Xepozz\FeatureFlag\FlagStorageInterface;

class Controller
{
    public function index(FlagStorageInterface $flagStorage)
    {
        if ($flagStorage->isActive('feature_name')) {
            // feature is enabled
        } else {
            // feature is disabled
        }
    }
}

use Xepozz\FeatureFlag\FlagStorageInterface;

class Controller
{
    public function index(FlagStorageInterface $flagStorage)
    {
        if ($condition) {
            $flagStorage->setFlag('feature_name', true);
        }
    }
}

use Xepozz\FeatureFlag\FlagStorageInterface;

class Controller
{
    public function index(FlagStorageInterface $flagStorage)
    {
        $flags = $flagStorage->getAll();
        // ...
    }
}