website logo
⌘K
Floodgate Documentation Portal
🚀GETTING STARTED
Quick Start Tutorial
Basic Concepts
🚩MANAGING FLAGS
Feature Flag Dashboard
Creating a Feature Flag
Environment Flag Dashboard
Environment Flag Settings
Feature Flag Kill Switch
Updating a Feature Flag
Deleting a Feature Flag
🖥️APPLICATIONS
About Applications
Creating New Applications
Switching Applications
⛅ENVIRONMENTS
About Environments
Creating New Environments
💗MANAGING YOUR TEAM
Team Members Dashboard
Managing Team Member Permissions
Inviting Team Members
⚙️ADMINISTRATION
Managing Subscriptions
Usage
🔌INTEGRATIONS
Microsoft Teams
Slack
🛠️DEVELOPMENT
The User Object
✔️SDKs
Floodgate SDK Overview
.NET SDK Documentation
Java SDK Documentation
PHP SDK Documentation
Node.js SDK Documentation
JavaScript SDK Documentation
Docs powered by archbee 
13min

PHP SDK Documentation

The guide will walk you through the installation and usage of the Floodgate PHP SDK.

Requirements

The PHP SDK requires PHP 5.3+

Getting Started

The first step is to install the Floodgate SDK as a dependency in your application using your application's dependency manager.

Shell
|
composer require floodgate/php-sdk


Now you have the Floodgate SDK installed and imported you can create a new instance of the Floodgate Client.

PHP
|
$floodgateClient = new \FloodgateSDK\FloodgateClient\FloodgateClient("ENTER-YOUR-SDK-KEY");


Evaluating a Flag

Using the $floodgateClient object you can now evaluate a flag using the GetValue method. The GetValue method requires two parameters with an optional third.

Property

Required

Description

Key

Yes

This is the Flag Key which you entered when creating the flag.

Default Value

Yes

This is the value which you want the flag to evaluate to if no flag data can be found.

User Object

No

This is an optional value which can be passed containing information about the user. This information is required to evaluate flags when doing user targeting or percentage rollout releases.

The example below shows an evaluation of a flag called buy-now-button-colour passing in a default value of orange. We are then going to dynamically change the background colour of a button with the Id of buy-now.

PHP
|
$sendPush = $floodgateClient->GetValue('send-push', 'off');

if ($sendPush == 'on')
{
  // Send push notification
}
else
{
  // Send email
}


Customizing the Floodgate Client

The Floodgate Client offers the following customization options to help with your implementation.

Option

Description

Timeout

By default the Floodgate Client will timeout after 10 seconds on startup. What this means is the client cannot obtain the necessary flag configuration data needed for operation it will throw an exception which you can handle in your application. You can use the Timeout property to override that value. For example you may wish to wait 2 seconds only before having the timeout exception thrown. The Timeout value is expressed in milliseconds.

Logger

The Floodgate SDK comes with 3 build in logging methods. This is useful if you want to debug what is happening inside the client. By default the DefaultLogger is used, this is actually a NULL logger and will simply ignore any log messages sent to it. You can also use the FileLogger and ConsoleLogger to log to the file system or console respectively.

Cache

The Floodgate Client stores and evaluates your flag configurations locally. You can set the cache you want the Floodgate Client to use depending on the PHP Framework you are using. The cache needs to implement the PSR16 Simple Cache Interface.

In the example below we are setting the startup Timeout value to 2 seconds, we will be logging the SDK output to a file located at "/path/to/log/file" and we are going to be using the default cache as configured in the CakePHP framework.

PHP
|
$config = [
  'timeout' => 2,
  'logger' => new \FloodgateSDK\Loggers\FileLogger('/path/to/log/file'),
  'cache' => new \FloodgateSDK\Cache\FloodgateCache(\Cake\Cache\Cache::pool("default"))
]);

$floodgateClient = new \FloodgateSDK\FloodgateClient\FloodgateClient("ENTER-YOUR-API-KEY", $config);


Targeting users with the User Object

User targeting is a key aspect of getting the most benefit out of using feature flags. By defining users we are able to take advantage of features like user targeting and percentage rollouts (canary releases).

Security and User Data

Even though Floodgate requires data about your users to allow for targeted flag evaluations, this data is never sent to the Floodgate servers. All evaluations are done locally on your servers within your application.

Property

Required

Type

Description

User Id

Yes

String

Every user is required to be assigned a unique id. This could be a database id or the users email address for example. Session IDs or UUIDs are suited best.

Custom Attributes

No

Array

Each user can have any number of custom attributes assigned to them. This is a very powerful feature of Floodgate as it gives you the flexibility to evaluate flags against any data element that may belong to your users.

Below is an example of creating a Floodgate User Object.

PHP
|
$customAttributes = [
  "name" => "Peter Parker",
  "subscription" => "Gold",
  "country" => "UK",
  "role" => "Admin"
];

$user = new \FloodgateSDK\User\User("a-unique-id", $customAttributes);


Once we have created the User Object we can pass this into the GetValue method to have the flag evaluated specifically for the user as shown below.

PHP
|
$sendPush = $floodgateClient->GetValue('send-push', 'off', $user);


Remember

By design PHP's shared-nothing architecture means caching in PHP is not available by default. This means that flag data will be re-fetched on each request. We recommend you using the caching mechanism provided by your PHP framework. The Floodgate PHP SDK provides a cache option which conforms to the PSR16 Simple Cache Interface.

View the Code

All our SDKs are open source and you are free to check out what's going on inside them. In fact we encourage contributions from the Floodgate community. You can view the source code on GitHub.

Updated 03 Mar 2023
Did this page help you?
Yes
No
UP NEXT
Node.js SDK Documentation
Docs powered by archbee 
TABLE OF CONTENTS
Requirements
Getting Started
Evaluating a Flag
Customizing the Floodgate Client
Targeting users with the User Object
Security and User Data
Remember
View the Code