Feature Flags in PHP: How to Stop Fearing and Start Enabling Features

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Feature Flags in PHP: The Magic of Turning Features On and Off Without Deployment

\n\n

Imagine: you've written a new feature — a super-duper recommendation system. Everything is tested on your local machine, but as soon as the code hits production, all hell breaks loose. Users are furious, conversion drops, and you're frantically reverting commits. Sound familiar?

\n\n

Now imagine a different scenario. You roll out the same feature, but it's disabled by default. You enable it for 5% of users, check the metrics, and roll it out further. If something goes wrong — you simply disable it with one click. No deployment. No stress. This is the magic of Feature Flags (also known as feature toggles, feature flips).

\n\n

In this article on PythonLib, we'll break down how to implement feature flags in PHP from scratch. You'll learn not just the theory, but also get ready-to-use code snippets you can grab right now.

\n\n

What are Feature Flags and Why Do PHP Developers Need Them?

\n\n

A Feature Flag is a software switch that allows you to enable or disable application functionality without changing code or redeploying. Simply put: you write the code once, and decide whether to show it to the user on the fly.

\n\n

Top 3 Reasons to Use Feature Flags in PHP:

\n
    \n
  • Safe Deployment: you can roll out code at any time of day. If something breaks — just disable the flag.
  • \n
  • A/B Testing: show different interface versions to different user segments and compare metrics.
  • \n
  • Canary Releases: roll out a new feature to 1% of the audience first, then 10%, then 100%.
  • \n
\n\n
“Feature Flags are like a light switch. You can turn it on for the whole room, or just for one corner. And you don't need to rewire the wiring every time.”
\n\n

Simplest Implementation of Feature Flags in PHP

\n\n

Let's start with the most basic but working option. Let's say we have a config.php file that stores all flags:

\n\n
<?php\n// config.php\nreturn [\n    'features' => [\n        'new_checkout' => false,\n        'dark_mode'    => true,\n        'search_v2'    => false,\n    ],\n];\n
\n\n

And now a simple wrapper class for checking flags:

\n\n
<?php\nclass FeatureFlagManager\n{\n    private array $features;\n\n    public function __construct(array $config)\n    {\n        $this->features = $config['features'] ?? [];\n    }\n\n    public function isEnabled(string $flag): bool\n    {\n        return $this->features[$flag] ?? false;\n    }\n}\n\n// Usage:\n$manager = new FeatureFlagManager(require 'config.php');\n\nif ($manager->isEnabled('dark_mode')) {\n    echo '<link rel="stylesheet" href="dark.css">';\n}\n
\n\n

The downside of this approach is obvious: to toggle a flag, you need to change the config and redeploy the application. This is no better than committing code. We need dynamic management.

\n\n

Dynamic Feature Flags with Database Storage

\n\n

The most common pattern is to store flag states in a database or Redis. This way, you can change them via an admin panel without restarting the application.

\n\n

MySQL Table Structure

\n\n
CREATE TABLE feature_flags (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    flag_name VARCHAR(100) UNIQUE NOT NULL,\n    is_enabled TINYINT(1) DEFAULT 0,\n    description TEXT,\n    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP\n);\n
\n\n

Class for Database Interaction

\n\n
<?php\nclass DbFeatureFlags\n{\n    private PDO $pdo;\n    private array $cache = [];\n\n    public function __construct(PDO $pdo)\n    {\n        $this->pdo = $pdo;\n    }\n\n    public function isEnabled(string $fla                

Blogs

Book Recommendations