Installation
GrowthBook is available on Composer:Quick Usage
Loading Features
There are 2 ways to load features into the SDK. You can useinitialize with a Client Key and API Host. Or, you can manually fetch and cache feature flags and pass them in with the withFeatures method.
initialize method
Theinitialize method can fetch features from the GrowthBook API for you.
By default, there is no caching enabled. You can enable it by passing any PSR16-compatible instance into the withCache method.
Caching is required for production usage
withHttpClient method. Note - you’ll need to specify both an HttpClient and a RequestFactoryInterface implementation.
The initialize method takes 3 arguments:
$clientKey(required) - Get this from your SDK Connection in GrowthBook.$apiHost(optional) - Defaults tohttps://cdn.growthbook.io. If self-hosting GrowthBook, set this to your API host.$decryptionKey(optional) - Only required if you’ve enabled encryption for your SDK Connection.
withFeatures method
If you prefer to have full control over the fetching/caching behavior, you can use thewithFeatures method instead to pass an associative array of features into the SDK.
withSavedGroups method
If you’re using saved groups and not using theinitialize method, you’ll need to call withSavedGroups to provide the saved groups data to the SDK.
The Growthbook Class
TheGrowthbook class has a number of properties. These can be set using a Fluent interface or can be passed into a constructor using an associative array. Every property also has a getter method if needed. Here’s an example:
withFeatures) at any point to update properties.
Attributes
You can specify attributes about the current user and request. These are used for two things:- Feature targeting (e.g. paid users get one value, free users get another)
- Assigning persistent variations in A/B tests (e.g. user id “123” always gets variation B)
withAttributes method completely overwrites the attributes object. You can use array_merge if you only want to update a subset of fields:
Tracking Experiments
Any time an experiment is run to determine the value of a feature, you want to track that event in your analytics system. You can either do this via a callback function:Google Analytics
Segment
Mixpanel
Logging
GrowthBook can output log messages to help you debug your feature flags and experiments. We support any PSR-3 compatible logger. We implement a fluent interface (withLogger) as well as the standard LoggerAware interface (setLogger).
Using Features
There are 3 main methods for interacting with features.$growthbook->isOn("feature-key")returns true if the feature is on$growthbook->isOff("feature-key")returns false if the feature is on$growthbook->getValue("feature-key", "default")returns the value of the feature with a fallback
$growthbook->getFeature("feature-key") to get back a FeatureResult object with the following properties:
- value - The JSON-decoded value of the feature (or
nullif not defined) - on and off - The JSON-decoded value cast to booleans
- source - Why the value was assigned to the user. One of
unknownFeature,defaultValue,force, orexperiment - experiment - Information about the experiment (if any) which was used to assign the value to the user
- experimentResult - The result of the experiment (if any) which was used to assign the value to the user
Sticky Bucketing
By default GrowthBook does not persist assigned experiment variations for a user. We rely on deterministic hashing to ensure that the same user attributes always map to the same experiment variation. However, there are cases where this isn’t good enough. For example, if you change targeting conditions in the middle of an experiment, users may stop being shown a variation even if they were previously bucketed into it. Sticky Bucketing is a solution to these issues. You can provide a Sticky Bucket Service to the GrowthBook instance to persist previously seen variations and ensure that the user experience remains consistent for your users. A sampleInMemoryStickyBucketService implementation is provided for reference, but in production you will definitely want to implement your own version using a database, cookies, or similar for persistence.
Sticky Bucket documents contain three fields
attributeName- The name of the attribute used to identify the user (e.g.id,cookie_id, etc.)attributeValue- The value of the attribute (e.g.123)assignments- A dictionary of persisted experiment assignments. For example:{"exp1__0":"control"}
db object:
Inline Experiments
Instead of declaring all features up-front and referencing them by ids in your code, you can also just run an experiment directly. This is done with the$growthbook->runInlineExperiment method:
Inline Experiment Return Value
A call torunInlineExperiment returns an ExperimentResult object with a few useful properties:
inExperiment flag will be false if the user was excluded from being part of the experiment for any reason (e.g. failed targeting conditions).
The hashUsed flag will only be true if the user was randomly assigned a variation. If the user was forced into a specific variation instead, this flag will be false.

