PHP code example of open-feature / go-feature-flag-provider

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

    

open-feature / go-feature-flag-provider example snippets


use OpenFeature\Providers\GoFeatureFlag\config\Config;
use OpenFeature\Providers\GoFeatureFlag\GoFeatureFlagProvider;
use OpenFeature\implementation\flags\MutableEvaluationContext;
use OpenFeature\implementation\flags\Attributes;
use OpenFeature\OpenFeatureAPI;

$config = new Config('http://gofeatureflag.org', 'my-api-key');
$provider = new GoFeatureFlagProvider($config);

$api = OpenFeatureAPI::getInstance();
$api->setProvider($provider);
$client = $api->getClient();
$evaluationContext = new MutableEvaluationContext(
      "214b796a-807b-4697-b3a3-42de0ec10a37", 
      new Attributes(["email" => '[email protected]'])
  );

$value = $client->getBooleanDetails('integer_key', false, $evaluationContext);
if ($value) {
    echo "The flag is enabled";
} else {
    echo "The flag is disabled";
}

$value = $client->getBooleanDetails('integer_key', false, $evaluationContext);
if ($value) {
  echo "The flag is enabled";
} else {
  echo "The flag is disabled";
}

// Bool
$client->getBooleanDetails('my-flag-key', false, new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));
$client->getBooleanValue('my-flag-key', false, new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));

// String
$client->getStringDetails('my-flag-key', "default", new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));
$client->getStringValue('my-flag-key', "default", new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));

// Integer
$client->getIntegerDetails('my-flag-key', 1, new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));
$client->getIntegerValue('my-flag-key', 1, new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));

// Float
$client->getFloatDetails('my-flag-key', 1.1, new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));
$client->getFloatValue('my-flag-key', 1.1, new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));

// Object
$client->getObjectDetails('my-flag-key', ["default" => true], new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));
$client->getObjectValue('my-flag-key', ["default" => true], new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));