PHP code example of pablojoan / feature

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

    

pablojoan / feature example snippets



use PabloJoan\Feature\Features; // Import the namespace.

$featureConfigs = [
    'foo' => [
        'variants' => [
            'variant1' => 100, //100% chance this variable will be chosen
            'variant2' => 0    //0% chance this variable will be chosen
        ]
    ],
    'bar' => [
        'variants' => [
            'variant1' => 25, //25% chance this variable will be chosen
            'variant2' => 25, //25% chance this variable will be chosen
            'variant3' => 50 //50% chance this variable will be chosen
        ],
        'bucketing' => 'id' //same id string will always return the same variant
    ]
];

$features = new Features($featureConfigs);

$features->isEnabled(featureName: 'foo');         // true
$features->getEnabledVariant(featureName: 'foo'); // 'variant1'

    $features->isEnabled(featureName: 'my_feature')

    $features->getEnabledVariant(featureName: 'my_feature')

    if ($features->isEnabled(featureName: 'my_feature')) {
        // do stuff
    }

    switch ($features->getEnabledVariant(featureName: 'my_feature')) {
      case 'foo':
          // do stuff appropriate for the 'foo' variant
          break;
      case 'bar':
          // do stuff appropriate for the 'bar' variant
          break;
    }

    $isMyFeatureEnabled = $features->isEnabled(
        featureName: 'my_feature',
        id: 'unique_id_string'
    );

    $variant = $features->getEnabledVariant(
        featureName: 'my_feature',
        id: 'unique_id_string'
    );

    $server_config['foo'] = ['variants' => ['enabled' => 100]];

    $server_config['foo'] = ['variants' => ['enabled' => 0]];

    $server_config['foo'] = ['variants' => ['blue_background' => 100]];

    $server_config['foo'] = ['variants' => ['enabled' => 1]];

    $server_config['foo'] = [
       'variants' => [
           'blue_background'   => 1,
           'orange_background' => 1,
           'pink_background'   => 1,
       ],
    ];

    $server_config['foo'] = [
       'variants' => ['enabled' => 10]
    ];

    $server_config['foo'] = [
       'variants' => ['enabled' => 1],
       'bucketing' => 'random'
    ];

    $server_config['foo'] = [
       'variants' => ['enabled' => 40],
       'bucketing' => 'id'
    ];

    $server_config['foo'] = ['variants' => ['enabled' => 50]];

    $server_config['foo'] = [
       'variants' => [
           'blue_background'   => 20,
           'orange_background' => 20,
           'pink_background'   => 20
       ],
    ];