PHP Feature Flags

PHP Feature Flag Resources/Solutions

  • LaunchDarkly PHP Feature Flag SDK – LaunchDarkly
    • An installable feature flag software development kit for PHP apps.  This SDK harnesses the LaunchDarkly app and allows you to target users and manage rollouts.  Feature flags are evaluated in microseconds.
    • PHP Client Github
  • Etsy Feature Flagging – Etsy
    • “The Feature API is how we selectively enable and disable features at a very fine grain as well as enabling features for a percentage of users for operational ramp-ups and for A/B tests. A feature can be completely enabled, completely disabled, or something in between and can comprise a number of related variants.”
    • Github Repo 
    • NOTE: No longer supported
  • Dzunke Feature Flags – Symfony2 Feature Flag Bundle by dzunke
    • “The Bundle will allow you to implement Feature Flags to your Application. Please Note that there is no Interface available, so the Flags must be configured directly in Symfony-Configs.”
  • Qandidate Feature Flagging Library – Qandidate Labs
    • “At we developed a feature toggle library that allows us to easily work with feature toggles. The goal was to minimize the barrier to use the toggles and be able to flip toggles at runtime, so we added an API with a Redis backend and a graphical user interface to flip the switches.”
    • Github Repo 
  • Rollout (for PHP) – Richard Fullmer
  • PHP Feature Flags – Jay Fienberg
    • “A simple PHP class for setting feature flags via HTTP cookie, URI query parameter or IP address. This is a PHP class that reads feature flags in URL query strings, browser cookies, and/or client IP address. These are only “on” flags–they do not represent values or states beyond “on”. The idea is: when a flag is absent, it is off, When a flag is present, it is on. The function then allows you to test for specific flags being present / on, using the above $ff->hasFlag(‘your-flag-name’) syntax. Note that this library doesn’t help you set flags per se–it just reads them.”
    • Github Repo 
  • SoCloz Feature Flag Bundle – Matthieu Bride
    • “A bundle to manage feature flags & A/B tests for PHP.”
    • Github Repo 


LaunchDarkly PHP SDK Reference

This reference guide documents all of the methods available in LaunchDarkly’s PHP SDK, and explains in detail how these methods work. If you want to dig even deeper, LaunchDarkly’s SDKs are open source– head to our PHP SDK repository on GitHub.

Getting started

If you haven’t taken a look at our Quickstart guide yet, we recommend starting there to see how install our SDK into your PHP application.

Once the SDK is installed, you’ll want to create a single, shared instance of LDClient:

$client = new LaunchDarkly\LDClient("YOUR_SDK_KEY");

LDClient must be a singleton

It’s important to make this a singleton– internally, the client instance maintains internal state that allows us to serve feature flags without making any remote requests. Be sure that you’re not instantiating a new client with every request.

Configuring a cache provider

The PHP SDK uses Guzzle Cache Middleware to cache HTTP responses. By default, an in-memory array cache is used. In production, we highly recommend using a persistent store such as Memcached or Redis as the backing store for the cache. To set this up, configure the LDClient with a cache provider as described in the Doctrine Cache documentation. The following example demonstrates how to configure Guzzle to use Memcached:

$memcached = new Memcached();
$memcached->addServer('localhost', 11211);

$cacheDriver = new \Doctrine\Common\Cache\MemcachedCache();
$cacheStorage = new \Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage($cache);

$client = new LaunchDarkly\LDClient("YOUR_SDK_KEY", array("cache" => $cacheStorage));

Caching and PHP

PHP’s shared-nothing architecture means that the out-of-the-box in-memory HTTP cache used by our SDK won’t cache feature flags across requests.

In production, we strongly recommend configuring a persistent store such as Memcached or Redis if you’re using PHP.

Customizing your client

In the previous example, we passed a cache_storage option as an array to the client constructor. There are a few additional options you can set in this array. Here’s an example:

$client = new LaunchDarkly\LDClient("YOUR_SDK_KEY", array("cache_storage" => $cacheStorage, "connect_timeout" => 3));

We’ve set the client connect timeout to 3 seconds in addition to providing a custom cache storage provider. The complete list of customizable parameters is as follows:

  • connect_timeout: Must be a number, in seconds, and controls the connection timeout to LaunchDarkly
  • timeout: Must be a number, in seconds, and controls the end-to-end request timeout to LaunchDarkly
  • capacity: Must be a number, and controls the maximum size of the event buffer. LaunchDarkly sends events asynchronously, and buffers them for efficiency.

Sending events in PHP

The LaunchDarkly SDK sends data back to our server to record events from Track and Variation calls. On our other platforms, this data is sent asynchronously, so that it adds no latency to serving web pages.

Once again, PHP’s shared-nothing architecture makes this a bit difficult. By default, LaunchDarkly forks an external process that executes curl to send this data. In practice we’ve found that this is the most reliable way to send data without introducing latency to page load times.

If your server does not have curl installed, or has other restrictions that make it impossible to invoke curlas an external process, you may need to implement a custom EventProcessor to send events to LaunchDarkly.


Feature flag targeting and rollouts are all determined by the user you pass to your Variation calls. In our PHP SDK, we use a builder pattern to make it easy to construct users. Here’s an example:

$user = (new LDUserBuilder("aa0ceb"))->firstName("Ernestina")->lastName("Evans")->email("")->custom(["groups" => array("Google","Microsoft")])->build();

Let’s walk through this snippet. The first argument to the builder is the user’s key– in this case we’ve used the hash"aa0ceb". The user key is the only mandatory user attribute. The key should also uniquely identify each user. You can use a primary key, an e-mail address, or a hash, as long as the same user always has the same key. We recommend using a hash if possible.

All of the other attributes (like firstName, email, and the custom attributes) are optional. The attributes you specify will automatically appear on our dashboard, meaning that you can start segmenting and targeting users with these attributes.

Besides the key, LaunchDarkly supports the following attributes at the “top level”. Remember, all of these are optional:

  • ip: Must be an IP address. If you provide an IP, LaunchDarkly will use a geolocation service to automatically infer a country for the user (unless you’ve already specified one).
  • firstName: Must be a string. If you provide a first name, you can search for users on the Users page by name.
  • lastName: Must be a string. If you provide a last name, you can search for users on the Users page by name.
  • country: Must be a string representing the country associated with the user.
  • email: Must be a string representing the user’s e-mail address. If an avatar URL is not provided, we’ll useGravatar to try to display an avatar for the user on the Users page.
  • avatar: Must be an absolute URL to an avatar image for the user.
  • name: Must be a string. You can search for users on the User page by name
  • anonymous: Must be a boolean. See the section below on anonymous users for more details.

In addition to these, you can pass us any of your own user data by passing custom attributes, like the groupsattribute in the example above.

A note on types

Most of our built-in attributes (like names and e-mail addresses) expect string values. Custom attributes values can be strings, booleans (like true or false), numbers, or lists of strings, booleans or numbers.

If you enter a custom value on our dashboard that looks like a number or a boolean, it’ll be interpreted that way. The PHP SDK is strongly typed, so be aware of this distinction.

Custom attributes are one of the most powerful features of LaunchDarkly. They let you target users according to any data that you want to send to us– organizations, groups, account plans– anything you pass to us becomes available instantly on our dashboard.

Anonymous users

You can also distinguish logged-in users from anonymous users in the SDK, as follows:

$user = (new LDUserBuilder("aa0ceb"))->anonymous(true)->build();

You will still need to generate a unique key for anonymous users– session IDs or UUIDs work best for this.

Anonymous users work just like regular users, except that they won’t appear on your Users page in LaunchDarkly. You also can’t search for anonymous users on your Features page, and you can’t search or autocomplete by anonymous user keys. This is actually a good thing– it keeps anonymous users from polluting your Users page!


The variation method determines which variation of a feature flag a user receives.

$client->variation($key, $user, false);

variation calls take the feature flag key, an LDUser, and a default value.

The default value will only be returned if an error is encountered– for example, if the feature flag key doesn’t exist or the user doesn’t have a key specified.

The variation call will automatically create a user in LaunchDarkly if a user with that user key doesn’t exist already. There’s no need to create users ahead of time (but if you do need to, take a look at Identify).

All flags

The allFlags method produces a dictionary of feature flag keys to their values for a specific user.

This method can be useful for passing feature flags to your front-end. In particular, it can be used to provide bootstrap flag settings for our JavaScript SDK.



The track method allows you to record actions your users take on your site. This lets you record events that take place on your server. In LaunchDarkly, you can tie these events to goals in A/B tests. Here’s a simple example:

$client->track("Signed up", user);

You can also attach custom data (anything that can be marshaled to JSON) to your event by passing an extra parameter to track:

$client->track("Completed purchase", ["price" => 320]);


The identify creates or updates users on LaunchDarkly, making them available for targeting and autocomplete on the dashboard. In most cases, you won’t need to call identify— the variation call will automatically create users on the dashboard for you. identify can be useful if you want to pre-populate your dashboard before launching any features.


Secure mode hash

The secureModeHash method computes an HMAC signature of a user signed with the client’s SDK key. If you’re using our JavaScript SDK for client-side flags, this method generates the signature you need for secure mode.


Offline mode

In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on premise, it might make sense to fall back to defaults when running on premise. setOffline lets you do this easily.

$client = new LaunchDarkly\LDClient("YOUR_SDK_KEY", array("offline" => true));
$client->variation("any.feature.flag", user, false); // will always return the default value (false)