LaunchDarkly PHP SDK 6.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  = '6.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.
getLogger()  : LoggerInterface
identify()  : void
Reports details about an evaluation context.
isOffline()  : bool
Returns whether the LaunchDarkly client is in offline mode.
migrationVariation()  : array<string|int, mixed>
This method returns the migration stage of the migration feature flag for the given evaluation context.
secureModeHash()  : string
Generates an HMAC sha256 hash for use in Secure mode.
track()  : void
Tracks that an application-defined event occurred.
trackMigrationOperation()  : void
Tracks the results of a migrations operation. This event includes measurements which can be used to enhance the observability of a migration within the LaunchDarkly UI.
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 = '6.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 $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

the evalation context

$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

getLogger()

public getLogger() : LoggerInterface
Return values
LoggerInterface

identify()

Reports details about an evaluation context.

public identify(LDContext $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

The context to register

Return values
void

isOffline()

Returns whether the LaunchDarkly client is in offline mode.

public isOffline() : bool
Return values
bool

migrationVariation()

This method returns the migration stage of the migration feature flag for the given evaluation context.

public migrationVariation(string $key, LDContext $context, Stage $defaultStage) : array<string|int, mixed>

This method returns the default stage if there is an error or the flag does not exist. If the default stage is not a valid stage, then a default stage of Stage::OFF will be used instead.

Parameters
$key : string
$context : LDContext
$defaultStage : Stage
Return values
array<string|int, mixed>

secureModeHash()

Generates an HMAC sha256 hash for use in Secure mode.

public secureModeHash(LDContext $context) : string

See: Secure mode

Parameters
$context : LDContext

The evaluation context

Return values
string

The hash value

track()

Tracks that an application-defined event occurred.

public track(string $eventName, LDContext $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

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

trackMigrationOperation()

Tracks the results of a migrations operation. This event includes measurements which can be used to enhance the observability of a migration within the LaunchDarkly UI.

public trackMigrationOperation(OpTracker $tracker) : void

Customers making use of the MigrationBuilder should not need to call this method manually.

Customers not using the builder should provide this method with the tracker returned from calling LDClient::migrationVariation.

Parameters
$tracker : OpTracker
Return values
void

variation()

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

public variation(string $key, LDContext $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

the evaluation context

$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 $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

the evaluation context

$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