简体   繁体   English

如何检查PECL扩展是否存在?

[英]How to check if PECL extension is present?

How do I from PHP code if a PECL extension is installed or not? 如果安装了PECL扩展,我如何从PHP代码?

I want gracefully handle the case when an extension is not installed. 我希望在没有安装扩展时优雅地处理这种情况。

I think the normal 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;
    }
}

get_loaded_extensions fits the bill. get_loaded_extensions适合账单。

Use like this: 使用这样:

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

你看过get_extension_funcs吗?

Couple different ways. 夫妻不同的方式。 You can just check for the existence of the class, or even a function: class_exists , function_exists , and get_extension_funcs : 您可以检查类的存在,甚至是函数: class_existsfunction_existsget_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 complicated, and use ReflectionExtension . 你也可以变得非常复杂,并使用ReflectionExtension When you construct it, it will throw a ReflectionException . 构造它时,它将抛出一个ReflectionException If it doesnt throw an exception, you can test for other things about the extension (like the 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM