如果已安装或未安装PECL扩展,如何从PHP代码进行转换?
当一个扩展没有安装时,我想优雅地处理这种情况。
发布于 2013-05-17 23:31:39
两种不同的方式。您可以只检查类是否存在,甚至可以检查函数是否存在: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
}你也可以变得非常复杂,并使用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
}发布于 2013-05-17 23:41:17
我认为正常的方式应该是使用extension-loaded。
if (!extension_loaded('gd')) {
// If you want to try load the extension at runtime, use this code:
if (!dl('gd.so')) {
exit;
}
}发布于 2013-05-17 23:33:25
https://stackoverflow.com/questions/16612794
复制相似问题