• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""


PHP SDK reference

Read time: 4 minutes
Last edited: Jun 13, 2023
Version 5 of the PHP SDK replaces users with contexts

A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users."

Code samples on this page are from the two most recent SDK versions where they differ. To learn more about upgrading, read PHP SDK 4.x to 5.0 migration guide.


This topic documents how to get started with the PHP SDK, and links to reference information on all of the supported features.

SDK quick links

LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:

SDK API documentationSDK API docs
GitHub repositoryphp-server-sdk
Sample applicationPHP
Published modulePackagist
SDK version compatibility

The LaunchDarkly PHP SDK, version 4.0 and higher, is compatible with PHP 7.3 and higher.

If you need support for older versions of PHP, use version 3.x of the LaunchDarkly PHP SDK.

Getting started

After you complete the Getting Started process, follow these instructions to start using the LaunchDarkly SDK in your PHP application.

The first step is to install Composer and the LaunchDarkly SDK as a dependency in your application. Refer to the SDK releases page to identify the latest version if you want to depend on a specific version.

To install Composer:

php composer.phar require launchdarkly/server-sdk
# In earlier versions, this was "launchdarkly/launchdarkly-php"

Then require Composer's autoloader:

require 'vendor/autoload.php';
The PHP SDK uses an SDK key

The PHP SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. To learn more about key types, read Keys.

After you install and import the SDK, create a single, shared instance of LDClient. Specify your SDK key here to authorize your application to connect to a particular environment within LaunchDarkly.

Only create one instance of client.

Here's how:

$client = new LaunchDarkly\LDClient("sdk-key-123abc");

You can use $client to check which variation a particular context will receive for a given feature flag.

Here's how:

$context = LDContext::builder("context-key-123abc")
if ($client->variation("your.flag.key", $context)) {
// application code to show the feature
} else {
// the code to run if the feature is off

In our Getting Started guide we recommend that users shut down the LaunchDarkly client on application termination. This step does not exist in PHP because the PHP SDK does not maintain long-lived network connections nor an event queue.

Fetching flags

There are two distinct methods of integrating LaunchDarkly in a PHP environment:

  • The Relay Proxy retrieves and stores flags in Redis, DynamoDB, or Consul. This is the recommended method. If you use Big Segments, you can only use Redis or DynamoDB as a persistent store.
  • Guzzle Cache Middleware requests and caches HTTP responses in an in-memory array. This is the default method.

We strongly suggest using the Relay Proxy. Per-flag caching mode using Guzzle is only intended for low-throughput environments.

Using the Relay Proxy

PHP's shared-nothing architecture prevents LaunchDarkly from reusing the streaming API connection across requests.

You can use PHP without the Relay Proxy, but we strongly recommend using the Relay Proxy in daemon mode if you are using PHP in a high-throughput setting. This makes the Relay Proxy receive feature flag updates.

To learn more, read Configuring SDKs to use different modes.

Using Guzzle

For the latest major version of the PHP SDK, use open-ended dependencies. For older versions, refer to your version's composer.json file.

To require Guzzle as a dependency:

php composer.phar require "guzzlehttp/guzzle:^6.3.0"
php composer.phar require "kevinrob/guzzle-cache-middleware:^1.4.0"

Guzzle is then used to fetch flags. You can persist your cache somewhere other than the default in-memory store, like Memcached or Redis.

You can then specify your cache when initializing the client with the cache option:

$client = new LaunchDarkly\LDClient("sdk-key-123abc", array("cache" => $cacheStorage));

Supported features

This SDK supports the following features: