我认为通常的方法是使用扩展加载.
if (!extension_loaded('gd')) {
// If you want to try load the extension at runtime, use this code:
if (!dl('gd.so')) {
exit;
}
}
Run Code Online (Sandbox Code Playgroud)
使用这样:
$ext_loaded = in_array('redis', get_loaded_extensions(), true);
Run Code Online (Sandbox Code Playgroud)
结合不同的方式。您可以只检查类是否存在,甚至是函数:class_exists、function_exists和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
}
Run Code Online (Sandbox Code Playgroud)
您也可以变得超级复杂,并使用ReflectionExtension. 当你构造它时,它会抛出一个ReflectionException. 如果它没有抛出异常,您可以测试有关扩展的其他信息(例如版本)。
<?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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14644 次 |
| 最近记录: |