|
| 1 | +--- |
| 2 | +title: PHP Feature Flags |
| 3 | +description: Set up Datadog Feature Flags for PHP applications. |
| 4 | +further_reading: |
| 5 | +- link: "/feature_flags/server/" |
| 6 | + tag: "Documentation" |
| 7 | + text: "Server-Side Feature Flags" |
| 8 | +- link: "/tracing/trace_collection/dd_libraries/php/" |
| 9 | + tag: "Documentation" |
| 10 | + text: "PHP Tracing" |
| 11 | +--- |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +This page describes how to instrument your PHP application with the Datadog Feature Flags SDK. The PHP SDK integrates with [OpenFeature][1], an open standard for feature flag management, and uses the Datadog tracer's Remote Configuration to receive flag updates in real time. |
| 16 | + |
| 17 | +This guide explains how to install and enable the SDK, create an OpenFeature client, and evaluate feature flags in your application. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +Before setting up the PHP Feature Flags SDK, ensure you have: |
| 22 | + |
| 23 | +- **Datadog Agent** with [Remote Configuration][2] enabled |
| 24 | +- **Datadog PHP tracer** `ddtrace` version 1.16.0 or later |
| 25 | +- **OpenFeature PHP SDK** `open-feature/sdk` version 2.0 or later |
| 26 | + |
| 27 | +Set the following environment variables: |
| 28 | + |
| 29 | +{{< code-block lang="bash" >}} |
| 30 | +# Required: Enable the feature flags provider |
| 31 | +export DD_EXPERIMENTAL_FLAGGING_PROVIDER_ENABLED=true |
| 32 | + |
| 33 | +# Required: Service identification |
| 34 | +export DD_SERVICE=<YOUR_SERVICE_NAME> |
| 35 | +export DD_ENV=<YOUR_ENVIRONMENT> |
| 36 | +{{< /code-block >}} |
| 37 | + |
| 38 | +## Installation |
| 39 | + |
| 40 | +Install the Datadog PHP tracer and OpenFeature SDK: |
| 41 | + |
| 42 | +{{< code-block lang="bash" >}} |
| 43 | +# Install the Datadog PHP tracer |
| 44 | +php datadog-setup.php --php-bin=all |
| 45 | + |
| 46 | +# Install the OpenFeature SDK |
| 47 | +composer require open-feature/sdk |
| 48 | +{{< /code-block >}} |
| 49 | + |
| 50 | +Or add the OpenFeature SDK to your `composer.json`: |
| 51 | + |
| 52 | +{{< code-block lang="json" filename="composer.json" >}} |
| 53 | +{ |
| 54 | + "require": { |
| 55 | + "open-feature/sdk": "^2.0" |
| 56 | + } |
| 57 | +} |
| 58 | +{{< /code-block >}} |
| 59 | + |
| 60 | +## Initialize the SDK |
| 61 | + |
| 62 | +Register the Datadog OpenFeature provider with the OpenFeature API. The provider connects to the Datadog tracer's Remote Configuration system to receive flag configurations. |
| 63 | + |
| 64 | +{{< code-block lang="php" >}} |
| 65 | +<?php |
| 66 | +
|
| 67 | +use OpenFeature\API; |
| 68 | +use DDTrace\OpenFeature\DataDogProvider; |
| 69 | +
|
| 70 | +// Create and register the Datadog provider |
| 71 | +$provider = new DataDogProvider(); |
| 72 | +API::setProvider($provider); |
| 73 | +
|
| 74 | +// Create an OpenFeature client |
| 75 | +$client = API::getClient(); |
| 76 | +
|
| 77 | +// Your application code here |
| 78 | +{{< /code-block >}} |
| 79 | + |
| 80 | +## Set the evaluation context |
| 81 | + |
| 82 | +Define an evaluation context that identifies the user or entity for flag targeting. The evaluation context includes attributes used to determine which flag variations should be returned: |
| 83 | + |
| 84 | +{{< code-block lang="php" >}} |
| 85 | +use OpenFeature\implementation\flags\EvaluationContext; |
| 86 | +use OpenFeature\implementation\flags\Attributes; |
| 87 | + |
| 88 | +$context = new EvaluationContext( |
| 89 | + 'user-123', // Targeting key (typically user ID) |
| 90 | + new Attributes([ |
| 91 | + 'email' => 'user@example.com', |
| 92 | + 'country' => 'US', |
| 93 | + 'tier' => 'premium', |
| 94 | + 'age' => 25, |
| 95 | + ]) |
| 96 | +); |
| 97 | +{{< /code-block >}} |
| 98 | + |
| 99 | +The targeting key is used for consistent traffic distribution (percentage rollouts). Additional attributes enable targeting rules, such as "enable for users in the US" or "enable for premium tier users" in the example above. |
| 100 | + |
| 101 | +## Evaluate flags |
| 102 | + |
| 103 | +After setting up the provider and creating a client, you can evaluate flags throughout your application. Flag evaluation is local and fast—the SDK uses locally cached configuration data, so no network requests occur during evaluation. |
| 104 | + |
| 105 | +Each flag is identified by a key (a unique string) and can be evaluated with a typed method that returns a value of the expected type. If the flag doesn't exist or cannot be evaluated, the SDK returns the provided default value. |
| 106 | + |
| 107 | +### Boolean flags |
| 108 | + |
| 109 | +Use `getBooleanValue` for flags that represent on/off or true/false conditions: |
| 110 | + |
| 111 | +{{< code-block lang="php" >}} |
| 112 | +$enabled = $client->getBooleanValue('new-checkout-flow', false, $context); |
| 113 | + |
| 114 | +if ($enabled) { |
| 115 | + showNewCheckout(); |
| 116 | +} else { |
| 117 | + showLegacyCheckout(); |
| 118 | +} |
| 119 | +{{< /code-block >}} |
| 120 | + |
| 121 | +### String flags |
| 122 | + |
| 123 | +Use `getStringValue` for flags that select between multiple variants or configuration strings: |
| 124 | + |
| 125 | +{{< code-block lang="php" >}} |
| 126 | +$theme = $client->getStringValue('ui-theme', 'light', $context); |
| 127 | + |
| 128 | +if ($theme === 'dark') { |
| 129 | + setDarkTheme(); |
| 130 | +} else { |
| 131 | + setLightTheme(); |
| 132 | +} |
| 133 | +{{< /code-block >}} |
| 134 | + |
| 135 | +### Numeric flags |
| 136 | + |
| 137 | +For numeric flags, use `getIntegerValue` or `getFloatValue`. These are appropriate when a feature depends on a numeric parameter such as a limit, percentage, or multiplier: |
| 138 | + |
| 139 | +{{< code-block lang="php" >}} |
| 140 | +$maxItems = $client->getIntegerValue('cart-max-items', 20, $context); |
| 141 | + |
| 142 | +$discountRate = $client->getFloatValue('discount-rate', 0.0, $context); |
| 143 | +{{< /code-block >}} |
| 144 | + |
| 145 | +### Object flags |
| 146 | + |
| 147 | +For structured data, use `getObjectValue`. This returns an array with complex configuration: |
| 148 | + |
| 149 | +{{< code-block lang="php" >}} |
| 150 | +$config = $client->getObjectValue('feature-config', [ |
| 151 | + 'maxRetries' => 3, |
| 152 | + 'timeout' => 30, |
| 153 | +], $context); |
| 154 | + |
| 155 | +$maxRetries = $config['maxRetries'] ?? 3; |
| 156 | +$timeout = $config['timeout'] ?? 30; |
| 157 | +{{< /code-block >}} |
| 158 | + |
| 159 | +### Flag evaluation details |
| 160 | + |
| 161 | +When you need more than just the flag value, use the `*Details` methods. These return both the evaluated value and metadata explaining the evaluation: |
| 162 | + |
| 163 | +{{< code-block lang="php" >}} |
| 164 | +$details = $client->getBooleanDetails('new-feature', false, $context); |
| 165 | + |
| 166 | +echo "Value: " . var_export($details->getValue(), true) . "\n"; |
| 167 | +echo "Reason: " . $details->getReason() . "\n"; |
| 168 | +echo "Variant: " . $details->getVariant() . "\n"; |
| 169 | +{{< /code-block >}} |
| 170 | + |
| 171 | +Flag details help you debug evaluation behavior and understand why a user received a given value. |
| 172 | + |
| 173 | +### Evaluation without context |
| 174 | + |
| 175 | +You can evaluate flags without providing an evaluation context. This is useful for global flags that don't require user-specific targeting: |
| 176 | + |
| 177 | +{{< code-block lang="php" >}} |
| 178 | +// Global feature flag - no context needed |
| 179 | +$maintenanceMode = $client->getBooleanValue('maintenance-mode', false); |
| 180 | + |
| 181 | +if ($maintenanceMode) { |
| 182 | + echo "Service temporarily unavailable"; |
| 183 | + exit; |
| 184 | +} |
| 185 | +{{< /code-block >}} |
| 186 | + |
| 187 | +## Troubleshooting |
| 188 | + |
| 189 | +### Provider not enabled |
| 190 | + |
| 191 | +If you receive warnings about the provider not being enabled, ensure `DD_EXPERIMENTAL_FLAGGING_PROVIDER_ENABLED=true` is set in your environment: |
| 192 | + |
| 193 | +{{< code-block lang="bash" >}} |
| 194 | +export DD_EXPERIMENTAL_FLAGGING_PROVIDER_ENABLED=true |
| 195 | +{{< /code-block >}} |
| 196 | + |
| 197 | +### Remote Configuration not working |
| 198 | + |
| 199 | +Verify the following to ensure that Remote Configuration is working: |
| 200 | +- Datadog Agent is version 7.55 or later |
| 201 | +- Remote Configuration is enabled on the Agent |
| 202 | +- `DD_SERVICE` and `DD_ENV` environment variables are set |
| 203 | +- The tracer can communicate with the Agent |
| 204 | + |
| 205 | +[1]: https://openfeature.dev/ |
| 206 | +[2]: /agent/remote_config/ |
| 207 | + |
| 208 | +## Further reading |
| 209 | + |
| 210 | +{{< partial name="whats-next/whats-next.html" >}} |
0 commit comments