Feature flagging approaches(with a code example in PHP - Laravel)

Mohammad Mahabadi
Trengo — Product & Tech
5 min readOct 1, 2022

So the story behind this article is that I wanted to present a proposal to launch a feature flagging in our company/project.

Let’s see first what is “Feature flagging”. (based on optimizely.com)

Feature flags (also known as feature toggles or feature switches) are a software development technique that turns certain functionality on and off during runtime, without deploying new code. This allows for better control and more experimentation over the full lifecycle of features.

Before we start, this article is only my opinion and it’s based on my experience so feel free to make it even better or leave your comment below for improving it!

Three ways to implement this (This is only my opinion — I believe there are even more than these three):

1- Use 3rd parties

2- Make a small service/package yourself

3- Put hard-coded values within your code

In each way, some aspects should that should be considered when we want to decide about feature flagging (This is only my opinion — I believe there are even more than these five):

1- Cost

2- Maintainability

3- Security

4- Traffic

5- Reports

Let’s start with the first approach, which is;

1. Use 3rd parties

This way you should use 3rd party companies or existing packages!

If you Google the feature flagging, you’ll find many SaaS (or non-SaaS) companies that are providing Feature flagging, so you can simply buy their services and use them as easy/fast as possible.

How to implement/use it? You should check if there’s an API or SDK available from that company then you can easily integrate it into your system, therefore does not matter what technologies/programming languages you’re using.

What are the pros and cons then?

Pros:

  • No maintenance needed
  • Other features can already be there, e.g A/B Testing
  • Having reports

Cons:

  • No integration with your internal administration tool (e.g Nova)
  • Another software is being added to your company
  • Monthly cost (money-wise)
  • External API requests

I’d say this is one of the best solutions for Enterprise companies! although it has some monthly cost, it’s almost nothing compared to the efforts that it needs to be placed.

2. Make a small service/package yourself

This is the way that I personally was into it but after having some research and real-world implementation I faced some advantages and also disadvantages.

So this also does not matter what kind of programming languages or technologies you’re using.

All you need to do is to start making a package (like a Laravel/Vue package) that is customized for your own company and you and your colleagues can contribute it based on your company’s requirements!

Let’s see this ready-to-use Laravel package: https://github.com/thisismahabadi/laravel-feature-flags

If you’re familiar with or using PHP, you should know about the trait concept, all you need to do is to install this package on your Laravel project, use that feature flagging trait wherever you need it, and then put the required data into the relevant database table and it works!

What do I mean by required data? The feature name, the expiration date, the user (or whatever resource) that needs to access that feature, and that’s it!

A good point about this package is that it’s not customized for my company and can be used globally and publicly so you can also do the same.

Also, it’s good to mention that there are a lot of ready-to-use packages and libraries that are shared free and publicly so you can check and explore them on Github or other places for your technology, programming language, or framework!

So let’s get back to the main topic, What were the advantages and disadvantages of this way?

Pros:

  • Customized based on your company's requirements

Cons:

  • Maintenance needed (cost resource)
  • Lack of having reports

So I think it’s clear that it needs maintenance which means the developers of the company have to put some time into that customized service/package to customize it every time the business changes or needs something new!

In my opinion, this way is good for small companies, small groups/teams, or even side projects.

3. Put hard-coded values within your code

This approach is a quick win for companies if they’re in hurry!

I think the clearest thing when thinking about feature flagging is some hard-coded values inside the code.

So imagine you’re dealing with users, what you need to do is to secure your code with a simple if-check condition to see what the user_id is and then make it accessible to the specific feature.

Or imagine if you’re working with anything other than the user, like agency level or team or anything else, it’s simply checking agency_id, team_id, or whatever.

Let me give you a code example:

Imagine you have a helpers.php within your framework or PHP project, so this is the function I’m mentioning:

So you only need to call this method featureFlagEnabledForAgencyId() within your code and just pass it the feature name and agency id to check if the requested user, agency, or whatever has access or not!

You wanna know what are the pros and cons?

Advantages:

  • Quick implementation

Disadvantages:

  • Require code changes (It’s not customized)
  • No reports

So as I said earlier, it’s very good for companies to implement feature flagging as soon as they need but it’s not a dynamic thing and should be replaced with the first (3rd parties), or second way (customized package/code).

As you see, I’m not talking about choosing one, the best approach, or recommending one of them, It’s totally up to you and your company’s requirements.

So that’s it, I hope this article helps you, Please comment your ideas, and questions below and share them with your friends and colleagues!

See you in the following article!

--

--