PHP Feature Detection Library

Link to GitHub Repository: PHPFeature

I had a short conversation on Twitter the other day with Andrey Savchenko ( @Rarst ). He was wondering whether feature-centric PHP requirements would work in WordPress extensions.

Although he was talking about the features of the “WP extensions”, I asked myself why there was no feature detection library available for PHP, similar to what Modernizr does for the browser features. Traditionally, PHP requirements are always based on a version number constant.

Meet the PHPFeature Library

PHPFeature is a first draft of such a PHP Feature Detection Library. So, instead of checking for a specific PHP version number (which forces you to know and compare which features were introduced by which PHP versions), you can simply tell the library what features you need, and it will tell you with a simple boolean value whether these are supported or not. You can also ask the library about the absolute minimum version required to support the set of features you’ve requested.

Why Bother?

What is the use of such a library? Well, for one, it simplifies knowing the required version you need for your code. Instead of building an entire matrix of version numbers, to find out what effective version is the most recent required one, you simply add the feature you intend to use to he array of features that gets passed in to the library.

The features that are included in the reference list could also include more minor stuff. Although pretty much everyone knows when namespaces were added, knowing when a specific bug was fixed might be more obscure data that is harder to come by. So the features you are checking the support for might also include specific bug-fixes.

The library could also be extended to include PHP extensions and libraries. So, instead of checking for a specific PHP version or an extension or a library, you could just check for the feature, and the library would tell you whether it is supported in any of its possible forms.

How Do I Use It?

To include the library in your project, you can use Composer:

composer require brightnucleus/phpfeature

Alternatively, you can copy the classes inside of your application and make sure that your application can find them.

Usage is pretty simple, it only comes with two methods so far.

public function is_supported( $features );

This returns a boolean value telling you whether all of the features that you passed in are supported by the current PHP version.

public function get_minimum_required( $features );

This returns a SemanticVersion object that contains the minimum PHP version that supports all of the features that you passed in.

Here’s an example as provided by the

 * PHPFeature example usage code

// Array of strings to define what features you need.
$features = array( 'namespaces', 'traits' );

// Instantiate the PHPFeature library.
// When you don't provide a version number as the first argument,
// the version of the currently used PHP interpreter is fetched.
$php = new PHPFeature();

// Check whether all of the features are supported. If not...
if ( ! $php->is_supported( $features ) ) {
    // ... throw exception and let user know the minimum needed.
    throw new RuntimeException( sprintf(
        'Your PHP interpreter does not support some features needed to run this application. Please upgrade to version %1$s or newer.',
        $php->get_minimum_required( $features )
    ) );

For more complex architectures, you could use this library to conditionally load certain files, or to conditionally extend classes with more advanced versions implementing the same interfaces.

Future Plans

This is more of a proof-of-concept, than a production-ready library at this stage. If there’s interest in using a library like this, though, I can see it becoming a standardized way of dealing with different PHP versions in your code.

One thing I would like to change is to lower the PHP requirement of the library itself to 5.2 (currently 5.3.2) so that it can be reliably used with WordPress.

If you think that this approach might be useful in any way and want to contribute, just let me know or provide pull requests, all contributions are welcome.

You can find the complete source code on GitHub:

Leave a Comment