Skip to content

Instantly share code, notes, and snippets.

@schlessera
Created January 14, 2016 09:22
Show Gist options
  • Save schlessera/7d0719511a3b20c8a1a9 to your computer and use it in GitHub Desktop.
Save schlessera/7d0719511a3b20c8a1a9 to your computer and use it in GitHub Desktop.
PHPFeature Example Usage Code.
<?php
/*
* 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 )
) );
}
@schlessera
Copy link
Author

PHPFeature is a first draft of a PHP Feature Detection Library similar to what Modernizr does for the browser features. So, instead of checking for a specific PHP version number (which forces you to know which and compare which features were introduced by which 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.
Read more about it here: http://www.alainschlesser.com/php-feature/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment