---
title: OptimizelyUserContext for the PHP SDK
slug: feature-experimentation/docs/optimizelyusercontext-php
docTags: 
createdAt: 2026-08-05T20:39:51.000Z
---

The OptimizelyUserContext object lets you make flag decisions and track events for a user context that you have already created using the [Create User Context](docId:6QxzVc8cTzURcLkeCt_sf) method.

## OptimizelyUserContext version

OptimizelyUserContext is supported on SDK v3.7 and higher.

## Forced decision methods version

`setForcedDecision()`, `getForcedDecision()`, `removeForcedDecision()` and `removeAllForcedDecisions()` methods are supported on v3.9.0 and higher.

## OptimizelyUserContext definition

The following code shows the object definition for OptimizelyUserContext:

```php
class OptimizelyUserContext implements \JsonSerializable
{
    // Create an instance of the Optimizely User Context. Pass in user id and optionally user attributes 
    public function __construct(Optimizely $optimizelyClient, $userId, array $attributes = [], $forcedDecisions = null);
    
    // get user id for the user
    public function getUserId();

    // set user attributes
    public function setAttribute($key, $value);
  
    // get user attributes
    public function getAttributes();

    // make a decision about which flag variation the user buckets into for the flag key
    public function decide($key, array $options = []);

    // make decisions about which flag variations the user buckets into for flag keys 
    public function decideForKeys(array $keys, array $options = []);

    // make decisions about which flag variations the user buckets into for all flags 
    public function decideAll(array $options = []);

    // track user event
    public function trackEvent($eventKey, array $eventTags = []);
    
    // Sets the forced decision (variationKey) for a given decision context
    public function setForcedDecision($context, $decision);

    // Returns the forced decision for a given decision context
    public function getForcedDecision($context);

    // Removes the forced decision for a given decision context
    public function removeForcedDecision($context);

    // Removes all forced decisions bound to this user context
    public function removeAllForcedDecisions();
}
```

## Properties

The following table shows attributes for the OptimizelyUserContext object:

| Attribute             | Type   | Comment                                                                                                                                                                                                  |
| --------------------- | ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| userId                | String | The ID of the user                                                                                                                                                                                       |
| (optional) attributes | Map    | A map of custom key-value pairs specifying attributes for the user that are used for [audience targeting](docId\:UDBvr5a8WHNkyOqzPhOr9). You can pass the map with the user ID when you create the user. |

## Methods

The following table shows methods for the OptimizelyUserContext object:

| Method                   | Comment                                                                                                                                                                                                                                                                                                        |
| ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| setAttribute             | Pass a custom user attribute as a key-value pair to the user context.                                                                                                                                                                                                                                          |
| decide                   | Returns a decision result for a flag key for a user. The decision result is returned in an OptimizelyDecision object, and contains all data required to deliver the flag rule.\ See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-php#example-decide) |
| decideForKeys            | Returns a map of flag decisions for specified flag keys.\ See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-php#example-decideforkeys)                                                                                                                |
| decideAll                | Returns decisions for all active (unarchived) flags for a user.\ See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-php#example-decideall)                                                                                                             |
| trackEvent               | Tracks a conversion event (in other words, an action a user takes) for a user. Logs an error message if the specified event key doesn't match any existing events.\ See \[Track Event]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/track-event-php)                                  |
| setForcedDecision        | Forces a user into a specific variation.\ See \[Set Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-php#set-forced-decision-method---setforceddecision)                                                                                         |
| getForcedDecision        | Returns what variation the user was forced into.\ See \[Get Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-php#get-forced-decision-method---getforceddecision)                                                                                 |
| removeForcedDecision     | Removes a user from a specific forced variation.\ See \[Remove Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-php#remove-forced-decision-method---removeforceddecision)                                                                        |
| removeAllForcedDecisions | Removes a user from all forced variations.\ See \[Remove All Forced Decisions]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-php#remove-all-forced-decisions-method---removeallforceddecisions)                                                                |

## See Also

[Create User Context](docId:6QxzVc8cTzURcLkeCt_sf)

## Source files

The language/platform source files containing the implementation for PHP is [Optimizely.php](https://github.com/optimizely/php-sdk/blob/master/src/Optimizely/Optimizely.php).
