OpenFeature Provider for PHP
Requirements
- PHP >= 8.1
Getting started
1. Install the provider with Composer
composer require configcat/openfeature-provider
2. Initialize the provider
The ConfigCatProvider
constructor takes the SDK key and an optional array
argument containing the additional configuration options for the ConfigCat PHP SDK:
use ConfigCat\ClientOptions;
use ConfigCat\Log\LogLevel;
use ConfigCat\OpenFeature\ConfigCatProvider;
use OpenFeature\implementation\flags\Attributes;
use OpenFeature\implementation\flags\EvaluationContext;
use OpenFeature\OpenFeatureAPI;
// Acquire an OpenFeature API instance.
$api = OpenFeatureAPI::getInstance();
// Build options for the ConfigCat SDK.
$options = [
ClientOptions::LOG_LEVEL => LogLevel::WARNING,
ClientOptions::CACHE_REFRESH_INTERVAL => 5,
//...
];
// Configure the provider.
$api->setProvider(new ConfigCatProvider('#YOUR-SDK-KEY#', $options));
// Create a client.
$client = $api->getClient();
For more information about all the configuration options, see the PHP SDK documentation.
3. Evaluate your feature flag
$isAwesomeFeatureEnabled = $client->getBooleanValue('isAwesomeFeatureEnabled', false);
if (is_bool($isAwesomeFeatureEnabled) && $isAwesomeFeatureEnabled) {
doTheNewThing();
} else {
doTheOldThing();
}
Evaluation Context
An evaluation context in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. The ConfigCat provider translates these evaluation contexts to ConfigCat User Objects.
The following table shows how the different context attributes are mapped to User Object attributes.
Evaluation context | User Object | Required |
---|---|---|
targetingKey | identifier | ☑ |
Email | email | |
Country | country | |
Any other | custom |
To evaluate feature flags for a context, use the OpenFeature Evaluation API:
$context = new EvaluationContext('#SOME-USER-ID#', new Attributes([
'Email' => '[email protected]',
'Country' => 'CountryID',
'Rating' => 4.5,
'RegisteredAt' => new DateTimeImmutable('2023-11-22T12:34:56.0000000Z'),
'Roles' => ['Role1', 'Role2']
]));
$isAwesomeFeatureEnabled = $client->getBooleanValue('isAwesomeFeatureEnabled', false, $context);