Simplifying PHP Feature Flags

Managing frequent software updates and patches has become the norm in agile software development. To streamline the process of feature rollouts, a reliable feature flagging solution is essential.

What are feature flags?

Feature flags, also known as feature switches or feature toggles, are boolean values assigned to specific features or components. They are used within a feature flagging system to enable or disable these features. By incorporating them into conditional statements in your code, you can control which features users can see or interact with. Feature flagging is possible in various programming languages.

To implement feature flags in a PHP application, there are some prerequisites to consider:

Prerequisites

  • A code editor (e.g., Visual Studio Code)
  • Composer version 2 (dependency management tool for PHP)
  • Node version 16+
  • NPM version 8+ (frontend package manager)
  • PHP version 7.4
  • Basic PHP knowledge

Implementing Feature Flagging in PHP

To simplify the process, you can use a hosted feature flag service like ConfigCat to create and manage your feature flags. Additionally, ConfigCat provides dashboard settings that allow you to target specific user segments based on characteristics, enabling you to deploy features selectively. For example, you can enable certain features only for users aged 25 to 35 living in France.

How to use the sample app

To demonstrate feature flagging in PHP, you can follow along with this sample PHP app that utilizes the Laravel framework and Vue.js for the frontend. The app includes a book list component that displays a list of books fetched from the backend. Feature flagging allows you to control the visibility of this component.

See also  How to Send SMS Using PHP

Here are the steps to build and run the sample app:

  1. Clone the code repository.
  2. Install PHP’s dependencies using the following command:
  3. Compile the frontend with NPM using this command:
  4. Start the PHP server with the following command:

Now, you can access the app by visiting ProgramMatek.

Notice the displayed list of books? Let’s create and configure a feature flag to enable or disable this component.

Creating a feature flag

To begin, sign up for a free ConfigCat account. Once signed up, follow these steps to create a feature flag:

  1. Create a feature flag with the following details:
    • FieldValue name: canShowBooks
    • FieldKey: canshowbooks
  2. Enable this feature by clicking the switcher on the right.

With the feature flag created and enabled, it’s time to integrate ConfigCat into your app to track its status.

Integrating with ConfigCat

For production, it is recommended to use the ConfigCat Client as a Singleton object. Follow these steps to integrate ConfigCat into your PHP app:

  1. Install the ConfigCat client SDK using Composer:
  2. Import the autoload.php file, which includes the previously installed SDK.
  3. Utilize the provided book controller API, which returns a list of books.

Testing the feature flag

To test the feature flag, follow these steps:

  1. Visit the ConfigCat dashboard and turn off the feature flag.
  2. Reload the page, and you should no longer see the list of books.

Snapshot of sample app - feature flag turned off

Final thoughts

In conclusion, integrating feature flags into your PHP app is a straightforward process that requires minimal effort. Feature flagging is particularly useful for development workflows that involve frequent releases of new features.

ConfigCat offers well-explained documentation, making it simple for you and your team to quickly implement its 10-minute trainable feature flag management interface.

See also  PHP Human Resources

ConfigCat supports multiple frameworks and languages, including PHP. To explore the full list of supported SDKs, click here.

Stay connected with ProgramMatek on Twitter, Facebook, LinkedIn, and GitHub for the latest updates.