PHP code example of featurevisor / featurevisor-php
1. Go to this page and download the library: Download featurevisor/featurevisor-php 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/ */
featurevisor / featurevisor-php example snippets
use Featurevisor\Featurevisor;
$f = Featurevisor::createFeaturevisor([
"datafile" => $datafileContent,
]);
use Featurevisor\Featurevisor;
$datafileUrl = "https://cdn.yoursite.com/datafile.json";
$datafileContent = file_get_contents($datafileUrl);
$datafileContent = json_decode($datafileContent, true);
$f = Featurevisor::createFeaturevisor([
"datafile" => $datafileContent
]);
$context = [
"userId" => "123",
"country" => "nl",
// ...other attributes
];
use Featurevisor\Featurevisor;
$f = Featurevisor::createFeaturevisor([
"context" => [
"deviceId" => "123",
"country" => "nl",
],
]);
$f->setContext([
"userId" => "123",
"country" => "nl",
]);
$f->setContext(
[
"deviceId" => "123",
"userId" => "234",
"country" => "nl",
"browser" => "chrome",
],
true // replace existing context
);
$context = [
"userId" => "123",
"country" => "nl",
];
$isEnabled = $f->isEnabled('my_feature', $context);
$variation = $f->getVariation('my_feature', $context);
$featureVariable = $f->getVariable('my_feature', 'my_variable', $context);
$globalVariable = $f->getVariable('supportEmail', $context);
$featureKey = 'my_feature';
$isEnabled = $f->isEnabled($featureKey);
if ($isEnabled) {
// do something
}
$isEnabled = $f->isEnabled($featureKey, [
// ...additional context
]);
$featureKey = 'my_feature';
$variation = $f->getVariation($featureKey);
if ($variation === "treatment") {
// do something for treatment variation
} else {
// handle default/control variation
}
$variation = $f->getVariation($featureKey, [
// ...additional context
]);
$variableKey = 'bgColor';
$bgColorValue = $f->getVariable($featureKey, $variableKey);
$bgColorValue = $f->getVariable($featureKey, $variableKey, [
// ...additional context
]);
$f->getVariableBoolean($featureKey, $variableKey, $context = []);
$f->getVariableString($featureKey, $variableKey, $context = []);
$f->getVariableInteger($featureKey, $variableKey, $context = []);
$f->getVariableDouble($featureKey, $variableKey, $context = []);
$f->getVariableArray($featureKey, $variableKey, $context = []);
$f->getVariableObject($featureKey, $variableKey, $context = []);
$f->getVariableJSON($featureKey, $variableKey, $context = []);
$email = $f->getVariable('supportEmail', ['country' => 'nl']);
$evaluation = $f->evaluateVariable('supportEmail', ['country' => 'nl']);
$settings = $f->getVariableObject('checkoutSettings');
$allEvaluations = $f->getFeatureEvaluations($context = []);
$globalVariables = $f->getVariableEvaluations($context = []);
print_r($allEvaluations);
// [
// myFeature: [
// enabled: true,
// variation: "control",
// variables: [
// myVariableKey: "myVariableValue",
// ],
// ],
//
// anotherFeature: [
// enabled: true,
// variation: "treatment",
// ]
// ]
use Featurevisor\Featurevisor;
$f = Featurevisor::createFeaturevisor([
"stickyFeatures" => [
"myFeatureKey" => [
"enabled" => true,
// optional
"variation" => 'treatment',
"variables" => [
"myVariableKey" => 'myVariableValue',
],
],
"anotherFeatureKey" => [
"enabled" => false,
],
],
"stickyVariables" => [
"supportEmail" => "[email protected] ",
],
]);
$f->setStickyFeatures(
[
"myFeatureKey" => [
"enabled" => true,
"variation" => 'treatment',
"variables" => [
"myVariableKey" => 'myVariableValue',
],
],
"anotherFeatureKey" => [
"enabled" => false,
],
],
// replace existing sticky features (false by default)
true
]);
$f->setDatafile($datafileContent);
$f->setDatafile($datafileContent, true);
$f = Featurevisor::createFeaturevisor([]);
function loadDatafile($f, string $target): void {
$url = "https://cdn.yoursite.com/production/featurevisor-$target.json";
$datafile = json_decode(file_get_contents($url), true);
// merges into whatever was loaded before
$f->setDatafile($datafile);
}
loadDatafile($f, 'products');
// later, when the user reaches checkout
loadDatafile($f, 'checkout');
$f = Featurevisor::createFeaturevisor([
"logLevel" => "debug",
]);
$f->setLogLevel("info");
$f = Featurevisor::createFeaturevisor([
"logLevel" => "info",
"onDiagnostic" => function (array $diagnostic) {
// send $diagnostic to your observability system
},
]);
$unsubscribe = $f->on('datafile_set', function ($event) {
$revision = $event['revision']; // new revision
$previousRevision = $event['previousRevision'];
$revisionChanged = $event['revisionChanged']; // true if revision has changed
$replaced = $event['replaced']; // true if datafile was replaced instead of merged
// list of feature keys that have new updates,
// and you should re-evaluate them
$features = $event['features'];
// handle here
});
// stop listening to the event
$unsubscribe();
$unsubscribe = $f->on('context_set', function ($event) {
$replaced = $event['replaced']; // true if context was replaced
$context = $event['context']; // the new context
echo "Context set";
});
$unsubscribe = $f->on('sticky_features_set', function ($event) {
$replaced = $event['replaced']; // true if sticky features got replaced
$features = $event['features']; // list of all affected feature keys
echo "Sticky features set";
});
$unsubscribeVariables = $f->on('sticky_variables_set', function ($event) {
$variables = $event['variables'];
});
$unsubscribe = $f->on('error', function ($event) {
echo $event['diagnostic']['message'];
});
// flag
$evaluation = $f->evaluateFlag($featureKey, $context = []);
// variation
$evaluation = $f->evaluateVariation($featureKey, $context = []);
// variable
$evaluation = $f->evaluateVariable($featureKey, $variableKey, $context = []);
$evaluation = $f->evaluateVariable('supportEmail', $context = []);
$myCustomModule = [
// only ',
// rest of the properties below are all optional per module
// setup receives a module API
'setup' => function ($api) {
$revision = $api['getRevision']();
$unsubscribe = $api['onDiagnostic'](function (array $diagnostic) {
// observe diagnostics reported by other modules or the SDK
});
$api['reportDiagnostic']([
'level' => 'info',
'code' => 'custom_module_ready',
'message' => 'Custom module is ready',
]);
},
// before any feature or global variable evaluation
'beforeEvaluation' => function ($options) {
$type = $options['type']; // `flag` | `variation` | `variable`
$featureKey = $options['featureKey'] ?? null;
$variableKey = $options['variableKey']; // if type is `variable`
$context = $options['context'];
// update context before evaluation
$options['context'] = array_merge($options['context'], [
'someAdditionalAttribute' => 'value',
]);
return $options;
},
// after any feature or global variable evaluation
'afterEvaluation' => function ($evaluation, $options) {
$reason = $evaluation['reason']; // `error` | `feature_not_found` | `variable_not_found` | ...
if ($reason === "error") {
// log error
return;
}
},
// configure bucket key
'bucketKey' => function ($options) {
$featureKey = $options['featureKey'];
$context = $options['context'];
$bucketBy = $options['bucketBy'];
$bucketKey = $options['bucketKey']; // default bucket key
// return custom bucket key
return $bucketKey;
},
// configure bucket value (between 0 and 100,000)
'bucketValue' => function ($options) {
$featureKey = $options['featureKey'];
$context = $options['context'];
$bucketKey = $options['bucketKey'];
$bucketValue = $options['bucketValue']; // default bucket value
// return custom bucket value
return $bucketValue;
},
// cleanup
'close' => function () {
// release module resources
},
];
use Featurevisor\Featurevisor;
$f = Featurevisor::createFeaturevisor([
'modules' => [
$myCustomModule
],
]);
$removeModule = $f->addModule($myCustomModule);
// $removeModule()
// or remove later by name
$f->removeModule('my-custom-module');
$childF = $f->spawn([
// user or request specific context
'userId' => '123',
]);
$isEnabled = $childF->isEnabled('my_feature');
$variation = $childF->getVariation('my_feature');
$variableValue = $childF->getVariable('my_feature', 'my_variable');
$globalVariableValue = $childF->getVariable('supportEmail');
$f->close();
use Featurevisor\OpenFeatureProvider;
use OpenFeature\OpenFeatureAPI;
use OpenFeature\implementation\flags\Attributes;
use OpenFeature\implementation\flags\EvaluationContext;
$provider = new OpenFeatureProvider([
'datafile' => $datafileContent,
]);
$api = OpenFeatureAPI::getInstance();
$api->setProvider($provider);
$client = $api->getClient();
$enabled = $client->getBooleanValue(
'checkout',
false,
new EvaluationContext('user-123', new Attributes(['country' => 'nl']))
);
$f->setStickyVariables(['supportEmail' => '[email protected] ']);
$provider = new OpenFeatureProvider(
options: ['datafile' => $datafileContent],
keySeparator: '/',
variationKey: '$variation'
);
$provider = new OpenFeatureProvider(
options: ['datafile' => $datafileContent],
globalVariablePrefix: 'global'
);
$provider = new OpenFeatureProvider(
options: ['datafile' => $datafileContent],
targetingKeyField: 'accountId'
);
$provider = new OpenFeatureProvider(
options: ['datafile' => $datafileContent],
onTrack: function ($name, $context, $details) {
echo $name;
}
);
$featurevisor = Featurevisor::createFeaturevisor(['datafile' => $datafileContent]);
$provider = new OpenFeatureProvider(featurevisor: $featurevisor);
$ composer bash
composer