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

EDIT ON GITHUB

PHP SDK reference

Read time: 2 minutes
Last edited: Sep 24, 2021

Overview

This reference guide documents all of the methods available in the PHP SDK, and explains in detail how these methods work. LaunchDarkly's SDKs are open source. To learn more, read PHP SDK repository on GitHub. The online API docs contain the programmatic definitions of every type and method. You can also try this SDK out by cloning and running a sample application.

Requirements

Version 4.0 or higher of the SDK requires PHP 7.3 or higher.

For older versions of PHP, use version 3.x of the 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:

1php composer.phar require launchdarkly/server-sdk
2
3# Note that in earlier versions, this was "launchdarkly/launchdarkly-php"

Then require Composer's autoloader:

1require 'vendor/autoload.php';

Once the SDK is installed, you'll want to create a single, shared instance of LDClient. You should specify your SDK key here so that your application will be authorized to connect to LaunchDarkly and for your application and environment.

To create a single instance:

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

Using $client, you can check which variation a particular user should receive for a given feature flag:

1$user = new LaunchDarkly\LDUser("user@test.com");
2
3if ($client->variation("your.flag.key", $user)) {
4 // application code to show the feature
5} else {
6 // the code to run if the feature is off
7}

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 store flags in Redis, DynamoDB, or Consul. This is the recommended method.
  • 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 re-using 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 Using the Relay Proxy in 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:

1php composer.phar require "guzzlehttp/guzzle:^6.3.0"
2php 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 could then specify your cache when initializing the client with the cache option:

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

Supported features

This SDK supports the following features: