PHP SDK
Install PHP SDK
You can find the latest version on our github Repository:
https://github.com/switchover-io/php-sdk
Install PHP SDK using composer
composer require switchover/php-sdk
and import it into your project:
require __DIR__ . '/vendor/autoload.php';
use Switchover\SwitchoverClient;
use Switchover\Context;
Initialize client
You will find your SDK Key on the environment page. Copy it and use it to initialize the client:
Basic usage:
// create a client, per default the client will cache the toggles 60 seconds
$client = new SwitchoverClient('<SDK-KEY');
//get some toggle values
$featureValue = $client->toggleValue('<TOGGLE-NAME>', false);
//or with context if you have specific (user) conditions
$context = new Context(array('email' => 'brandon.taylor@acme.com'));
$isFeatureVisible = $client->toggleValue('<OTHER-FLAG>', false, $context);
if ($isFeatureVisible) {
// ...do something
}
Of course it’s also possible to add some Options to the client:
$client = new SwitchoverClient('<SDK-KEY', [
'cache.time' => 10,
'http' => [
'timeout' => '10',
'proxy' => 'http://proxy.tld'
]
]);
Options
Option | Value |
---|---|
cache.time | Sets time in seconds before the internal cache becomes stale and will be refreshed (TTL). Default is 60 seconds. The value 0 will keep the cache forever. |
logger | Possibility to provide you own logger instance (PSR-7). |
cache | Option to set your cache instance (e.g. for redis). Expects a PSR-16 compliant instance. |
http | The client uses guzzlehttp/guzzle for http requests. You can pass an array of options to the Guzzle Http Client. |
Context Object
To check conditions in the client sdk a context object must be created and passed to the toggleValue method.
$contextArray = array("key" => "value");
$context = new Context($contextArray);
Public methods
toggleValue()
Parameters | Description |
name | the name of the feature toggle configured in switchover |
defaultValue | It will return the given default value: * – When toggle is INACTIVE * – When evalution fails * – When client was not fully initialized |
context | Context Object (array) |
$client->toggleValue(
string $name,
boolean $defaultValue,
Context $context = null
)
refresh()
Manually refreshes toggles.
$client->refresh();
getToggleKeys()
Get all toggle keys, which currently loaded
$client->getToggleKeys();