How to check if a PECL extension exists?

How do I get PHP code if the PECL extension is installed?

I want to handle the case when the extension is not installed.

+7
source share
4 answers

I think the usual way would be to use extension-loaded .

if (!extension_loaded('gd')) { // If you want to try load the extension at runtime, use this code: if (!dl('gd.so')) { exit; } } 
+7
source

get_loaded_extensions matches the score.

Use this:

 $ext_loaded = in_array('redis', get_loaded_extensions(), true); 
+5
source

Have you seen get_extension_funcs ?

+4
source

The couple is different. You can simply check for a class or even a function: class_exists , function_exists and get_extension_funcs :

 <?php if( class_exists( '\Memcached' ) ) { // Memcached class is installed } // I cant think of an example for `function_exists`, but same idea as above if( get_extension_funcs( 'memcached' ) === false ) { // Memcached isn't installed } 

You can also get super sophisticated and use ReflectionExtension . When you build it, it will throw a ReflectionException . If it does not throw an exception, you can check other things about the extension (e.g. version).

 <?php try { $extension = new \ReflectionExtension( 'memcached' ); } catch( \ReflectionException $e ) { // Extension Not loaded } if( $extension->getVersion() < 2 ) { // Extension is at least version 2 } else { // Extension is only version 1 } 
+2
source

All Articles