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;
$datafileUrl = "https://cdn.yoursite.com/datafile.json";
$datafileContent = file_get_contents($datafileUrl);
$datafileContent = json_decode($datafileContent, true);
$f = Featurevisor::createInstance([
"datafile" => $datafileContent
]);
$context = [
"userId" => "123",
"country" => "nl",
// ...other attributes
];
use Featurevisor\Featurevisor;
$f = Featurevisor::createInstance([
"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);
$variableValue = $f->getVariable('my_feature', 'my_variable', $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 = []);
$allEvaluations = $f->getAllEvaluations($context = []);
print_r($allEvaluations);
// [
// myFeature: [
// enabled: true,
// variation: "control",
// variables: [
// myVariableKey: "myVariableValue",
// ],
// ],
//
// anotherFeature: [
// enabled: true,
// variation: "treatment",
// ]
// ]
use Featurevisor\Featurevisor;
$f = Featurevisor::createInstance([
"sticky" => [
"myFeatureKey" => [
"enabled" => true,
// optional
"variation" => 'treatment',
"variables" => [
"myVariableKey" => 'myVariableValue',
],
],
"anotherFeatureKey" => [
"enabled" => false,
],
],
]);
$f->setSticky(
[
"myFeatureKey" => [
"enabled" => true,
"variation" => 'treatment',
"variables" => [
"myVariableKey" => 'myVariableValue',
],
],
"anotherFeatureKey" => [
"enabled" => false,
],
],
// replace existing sticky features (false by default)
true
]);
$f->setDatafile($datafileContent);
use Featurevisor\Featurevisor;
use Featurevisor\Logger;
$f = Featurevisor::createInstance([
"logger" => Logger::create([
"level" => "debug",
]),
]);
$f = Featurevisor::createInstance([
"logLevel" => "debug",
]);
$f->setLogLevel("debug");
use Featurevisor\Featurevisor;
use Featurevisor\Logger;
$f = Featurevisor::createInstance([
"logger" => Logger::create([
"level" => "info",
"handler" => function ($level, $message, $details) {
// do something with the log
},
]),
]);
$unsubscribe = $f->on('datafile_set', function ($event) {
$revision = $event['revision']; // new revision
$previousRevision = $event['previousRevision'];
$revisionChanged = $event['revisionChanged']; // true if revision has changed
// 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_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";
});
// flag
$evaluation = $f->evaluateFlag($featureKey, $context = []);
// variation
$evaluation = $f->evaluateVariation($featureKey, $context = []);
// variable
$evaluation = $f->evaluateVariable($featureKey, $variableKey, $context = []);
$myCustomHook = [
// only ',
// rest of the properties below are all optional per hook
// before evaluation
'before' => function (options) {
$type = $options['type']; // `feature` | `variation` | `variable`
$featureKey = $options['featureKey'];
$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 evaluation
'after' => 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;
},
];
use Featurevisor\Featurevisor;
$f = Featurevisor::createInstance([
'hooks' => [
$myCustomHook
],
]);
$removeHook = $f->addHook($myCustomHook);
// $removeHook()
$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');
$f->close();
$ composer