PHP Provider

Get Started

An OpenFeature Provider is a pluggable integration layer that connects the OpenFeature SDK to a specific feature flag management system (e.g., VWO or custom in-house solutions). OpenFeature is an open-source standard for feature flagging, designed to provide a vendor-agnostic approach, enabling organizations to switch between feature flagging tools without rewriting application code.

This VWO OpenFeature Provider for PHP helps you integrate Feature Experimentation systems into your PHP-based server applications.

GitHub Repo

Check this out

Published on Packagist

Check this out

OpenFeature Ecosystem

Check this out

OpenFeature Docs

Check this out

🚧

Note

This library is intended to be used in server-side contexts and has not been evaluated for use on mobile devices.

Requirements

PHP: 7.0+

SDK Installation

It's recommended you use composer to install dependencies

composer require vwo/vwo-openfeature-provider-php

Usage

use OpenFeature\OpenFeatureAPI;
use OpenFeature\implementation\flags\EvaluationContext;
use VWOOpenFeatureProvider\VWOProvider;
use vwo\VWO;

class OpenFeatureTest {
  public static function main() {
    // Initialize the VWO client options
    $vwoInitOptions = [
        'sdkKey' => 'your-sdk-key-here',     // Replace with your SDK Key
        'accountId' => 123456,               // Replace with your VWO Account ID
    ];

    // Initialize VWO Client
    $vwoClient = VWO::init($vwoInitOptions);
    if ($vwoClient === null) {
        echo "Failed to initialize VWO Client\n";
        return;
    }

    // Initialize the VWO provider
    $vwoProvider = new VWOProvider($vwoClient);

    // Set the provider using OpenFeature API
    $api = OpenFeatureAPI::getInstance();
    $api->setProvider($vwoProvider);

    // Call the test flags method to evaluate different flag types
    self::testFlags($api);
  }

  public static function testFlags(OpenFeatureAPI $api) {
    // Create custom variables for the context
    $customVariables = [
        'name' => 'Ashley'
    ];

    // Manually creating EvaluationContext with targetingKey and additional attributes
    $attributes = new OpenFeature\implementation\flags\Attributes([
        'key' => 'variable-key',
        'customVariables' => $customVariables, // Custom variables
    ]);

    $context = new EvaluationContext('userId1', $attributes);

    // Get the client from OpenFeature API
    $client = $api->getClient();

    // Test object flag
    $objectResult = $client->getObjectValue('f1',$customVariables, $context);
    echo "OBJECT result: " . json_encode($objectResult) . "\n";
  }
}

// Run the OpenFeatureTest script
OpenFeatureTest::main();

API Details

APIArgumentsArgument DescriptionAPI Description
VWO::init$vwoInitOptions: arraysdkKey: Unique key for authentication with VWO.
accountId: VWO account identifier.
Initializes the VWO client with the provided SDK key and account ID. Returns a VWO client instance or null if initialization fails.
new VWOProvider($vwoClient)$vwoClient: VWOvwoClient: The initialized VWO SDK client instance.Creates a new instance of VWOProvider, integrating VWO with OpenFeature.
OpenFeatureAPI::getInstance()NoneReturns a singleton instance of OpenFeatureAPI for managing feature flag evaluations.
$api->setProvider($vwoProvider)$vwoProvider: VWOProvidervwoProvider: The VWO provider instance that will handle feature flag evaluations.Sets the provider for OpenFeature, enabling it to evaluate feature flags using VWO.
new EvaluationContext($targetingKey, $attributes)$targetingKey: string, $attributes: AttributestargetingKey: Unique identifier for the user.
attributes: Object containing additional attributes (e.g., key, customVariables).
Creates an evaluation context with user-specific details and attributes for feature flag evaluation.
new Attributes($attributeArray)$attributeArray: arrayattributeArray: Key-value pairs of attributes (e.g., ['key' => 'variable-key', 'customVariables' => ['name' => 'Ashley']]).Initializes attributes for evaluation context, containing user-specific details.
$api->getClient()NoneReturns a client instance from OpenFeature API for evaluating feature flags.
$client->getObjectValue($featureKey, $defaultValue, $context)$featureKey: string, $defaultValue: mixed, $context: EvaluationContextfeatureKey: The unique key representing the feature flag.
defaultValue: The fallback value if the flag evaluation fails.
context: The evaluation context containing user details and attributes.
Fetches the object value of a feature flag. Uses context to retrieve user-specific values.