Skip to main content
Version: Next

PHP Provider

Packagist - Version Packagist - Downloads

In conjunction with the OpenFeature SDK you will be able to evaluate your feature flags in your Ruby applications.

Functionalities:

  • Manage the integration of the OpenFeature PHP SDK and GO Feature Flag relay-proxy.

Dependency Setup

Composer

composer require open-feature/go-feature-flag-provider

Getting started

Initialize the provider

The GoFeatureFlagProvider takes a config object as parameter to be initialized.

The constructor of the config object has the following options:

OptionDescription
endpoint(mandatory) The URL to access to the relay-proxy.
(example: https://relay.proxy.gofeatureflag.org/)
apiKeyThe token used to call the relay proxy.
customHeadersAny headers you want to add to call the relay-proxy.
httpclientThe HTTP Client to use (if you want to use a custom one). It has to be a PSR-7 compliant implementation.

The only required option to create a GoFeatureFlagProvider is the URL (endpoint) to your GO Feature Flag relay-proxy instance.

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('https://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" => 'contact@gofeatureflag.org'])
);

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

The evaluation context is the way for the client to specify contextual data that GO Feature Flag uses to evaluate the feature flags, it allows to define rules on the flag.

The targeting_key is mandatory for GO Feature Flag to evaluate the feature flag, it could be the id of a user, a session ID or anything you find relevant to use as identifier during the evaluation.

Evaluate a feature flag

The client is used to retrieve values for the current EvaluationContext. For example, retrieving a boolean value for the flag "my-flag":

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

GO Feature Flag supports different all OpenFeature supported types of feature flags, it means that you can use all the accessor directly

// 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"));

Features status

StatusFeatureDescription
Flag evaluationIt is possible to evaluate all the type of flags
CachingMechanism is in place to refresh the cache in case of configuration change
Event StreamingNot supported by the SDK
LoggingNot supported by the SDK
Flag MetadataNot supported by the SDK
Implemented: ✅ | In-progress: ⚠️ | Not implemented yet: ❌

Contributing

This project welcomes contributions from the community. If you're interested in contributing, see the contributors' guide for some helpful tips.

PHP Versioning

This library targets PHP version 8.0 and newer. As long as you have any compatible version of PHP on your system you should be able to utilize the OpenFeature SDK.

This package also has a .tool-versions file for use with PHP version managers like asdf.

Installation and Dependencies

Install dependencies with composer install, it will update the composer.lock with the most recent compatible versions.

We value having as few runtime dependencies as possible. The addition of any dependencies requires careful consideration and review.

Get the latest GO Feature Flag updates