LaunchDarkly PHP SDK 5.1.0

LDClient
in package

A client for the LaunchDarkly API.

Table of Contents

DEFAULT_BASE_URI  = 'https://sdk.launchdarkly.com'
DEFAULT_EVENTS_URI  = 'https://events.launchdarkly.com'
VERSION  = '5.1.0'
The current SDK version.
__construct()  : LDClient
Creates a new client instance that connects to LaunchDarkly.
allFlagsState()  : FeatureFlagsState
Returns an object that encapsulates the state of all feature flags for a given context.
flush()  : bool
Publishes any pending analytics events to LaunchDarkly.
identify()  : void
Reports details about an evaluation context or user.
isOffline()  : bool
Returns whether the LaunchDarkly client is in offline mode.
secureModeHash()  : string
Generates an HMAC sha256 hash for use in Secure mode.
track()  : void
Tracks that an application-defined event occurred.
variation()  : mixed
Calculates the value of a feature flag for a given context.
variationDetail()  : EvaluationDetail
Calculates the value of a feature flag for a given context, and returns an object that describes the way the value was determined.

Constants

DEFAULT_BASE_URI

public string DEFAULT_BASE_URI = 'https://sdk.launchdarkly.com'

DEFAULT_EVENTS_URI

public string DEFAULT_EVENTS_URI = 'https://events.launchdarkly.com'

VERSION

The current SDK version.

public string VERSION = '5.1.0'

Methods

__construct()

Creates a new client instance that connects to LaunchDarkly.

public __construct(string $sdkKey[, array<string|int, mixed> $options = [] ]) : LDClient
Parameters
$sdkKey : string

The SDK key for your account

$options : array<string|int, mixed> = []

Client configuration settings

  • base_uri: Base URI of the LaunchDarkly service. Change this if you are connecting to a Relay Proxy instance instead of directly to LaunchDarkly. To learn more, read The Relay Proxy.
  • events_uri: Base URI for sending events to LaunchDarkly. Change this if you are forwarding events through a Relay Proxy instance.
  • timeout: The maximum length of an HTTP request in seconds. Defaults to 3.
  • connect_timeout: The maximum number of seconds to wait while trying to connect to a server. Defaults to 3.
  • cache: An optional implementation of Guzzle's CacheStorageInterface. Defaults to an in-memory cache.
  • send_events: If set to false, disables the sending of events to LaunchDarkly. Defaults to true.
  • logger: An optional implementation of Psr\Log\LoggerInterface. Defaults to a Monolog\Logger sending all messages to the PHP error_log.
  • offline: If set to true, disables all network calls and always returns the default value for flags. Defaults to false.
  • feature_requester: An optional FeatureRequester implementation, or a class or factory for one. Defaults to Guzzle::featureRequester(). There are also optional packages providing database integrations; see Storing data.
  • event_publisher: An optional EventPublisher implementation, or a class or factory for one. Defaults to Curl::eventPublisher().
  • all_attributes_private: If set to true, no user attributes (other than the key) will be sent back to LaunchDarkly. Defaults to false.
  • private_attribute_names: An optional array of user attribute names to be marked private. Any users sent to LaunchDarkly with this configuration active will have attributes with these names removed. You can also set private attributes on a
  • application_info: An optional ApplicationInfo instance. per-user basis in the LDContext builder.
  • Other options may be available depending on any features you are using from the LaunchDarkly\Integrations namespace.
Return values
LDClient

allFlagsState()

Returns an object that encapsulates the state of all feature flags for a given context.

public allFlagsState(LDContext|LDUser $context[, array<string|int, mixed> $options = [] ]) : FeatureFlagsState

This includes the flag values as well as other flag metadata that may be needed by front-end code, since the most common use case for this method is bootstrapping in conjunction with the JavaScript browser SDK.

This method does not send analytics events back to LaunchDarkly.

Parameters
$context : LDContext|LDUser

the evalation context or user

$options : array<string|int, mixed> = []

Optional properties affecting how the state is computed:

  • clientSideOnly: Set this to true to specify that only flags marked for client-side use should be included; by default, all flags are included
  • withReasons: Set this to true to include evaluation reasons (see LDClient::variationDetail())
  • detailsOnlyForTrackedFlags: Set to true to omit any metadata that is normally only used for event generation, such as flag versions and evaluation reasons, unless the flag has event tracking or debugging turned on
Return values
FeatureFlagsState

a FeatureFlagsState object (will never be null)

flush()

Publishes any pending analytics events to LaunchDarkly.

public flush() : bool

This is normally done automatically by the SDK.

Return values
bool

Whether the events were successfully published

identify()

Reports details about an evaluation context or user.

public identify(LDContext|LDUser $context) : void

This method simply creates an analytics event containing the context properties, to that LaunchDarkly will know about that context if it does not already.

Evaluating a flag, by calling LDClient::variation() or LDClient::variationDetail() :func:variation_detail(), also sends the context information to LaunchDarkly (if events are enabled), so you only need to use identify() if you want to identify the context without evaluating a flag.

Parameters
$context : LDContext|LDUser

The context or user to register

Return values
void

isOffline()

Returns whether the LaunchDarkly client is in offline mode.

public isOffline() : bool
Return values
bool

secureModeHash()

Generates an HMAC sha256 hash for use in Secure mode.

public secureModeHash(LDContext|LDUser $context) : string

See: Secure mode

Parameters
$context : LDContext|LDUser

The evaluation context or user

Return values
string

The hash value

track()

Tracks that an application-defined event occurred.

public track(string $eventName, LDContext|LDUser $context[, mixed $data = null ][, int|float|null $metricValue = null ]) : void

This method creates a "custom" analytics event containing the specified event name (key) and context properties. You may attach arbitrary data or a metric value to the event with the optional data and metricValue parameters.

Note that event delivery is asynchronous, so the event may not actually be sent until later; see LDClient::flush().

Parameters
$eventName : string

The name of the event

$context : LDContext|LDUser

The evaluation context or user associated with the event

$data : mixed = null

Optional additional information to associate with the event

$metricValue : int|float|null = null

A numeric value used by the LaunchDarkly experimentation feature in numeric custom metrics; can be omitted if this event is used by only non-numeric metrics

Return values
void

variation()

Calculates the value of a feature flag for a given context.

public variation(string $key, LDContext|LDUser $context[, mixed $defaultValue = false ]) : mixed

If an error makes it impossible to evaluate the flag (for instance, the feature flag key does not match any existing flag), $defaultValue is returned.

Parameters
$key : string

the unique key for the feature flag

$context : LDContext|LDUser

the evaluation context or user

$defaultValue : mixed = false

the default value of the flag

Tags
see
LDClient::variationDetail()
Return values
mixed

The variation for the given context, or $defaultValue if the flag cannot be evaluated

variationDetail()

Calculates the value of a feature flag for a given context, and returns an object that describes the way the value was determined.

public variationDetail(string $key, LDContext|LDUser $context[, mixed $defaultValue = false ]) : EvaluationDetail

The "reason" property in the result will also be included in analytics events, if you are capturing detailed event data for this flag.

Parameters
$key : string

the unique key for the feature flag

$context : LDContext|LDUser

the evaluation context or user

$defaultValue : mixed = false

the default value of the flag

Return values
EvaluationDetail

An EvaluationDetail object that includes the feature flag value and evaluation reason

Search results