Home >Backend Development >PHP Problem >How to dynamically view and load PHP extensions

How to dynamically view and load PHP extensions

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-06-29 15:40:222682browse

After compiling and completing the configuration of php.ini, we successfully installed a PHP extension. However, PHP also provides us with two functions that can view the status of extensions during dynamic runtime and load extensions that are not configured in php.ini. Next, let's take a look at their use.

How to dynamically view and load PHP extensions

Check whether the extension has been loaded

echo extension_loaded("redis");

A very simple function, its function is to check whether an extension has been loaded. It returns a Boolean value that returns true if the extension has been loaded, false if the extension is not loaded.

In the PHP-FPM web page, we can use the phpinfo() function to view the current PHP status and extension-related information. In the CLI command line script, we can use the php -m command to view the loaded extensions.

Dynamic loading of extensions

First, we turn off the loading of redis extensions in php.ini, and also need to turn on enable_dl=1, so that we can use the dl() function to dynamically load One expanded.

dl("redis");
echo extension_loaded("redis");
// 1

Yes, the dl() function is a function used to dynamically load extensions. However, there are many restrictions on its use, and it is not a safe function. So in PHP7, its configuration enable_dl in php.ini is turned off by default. We also try not to use this method to load extensions in the production environment.

In addition, this function is only valid for the CLI environment in PHP7. In other words, in the PHP-FPM web environment, this function is useless, even if enable_dl in php.ini has been turned on.

The extension loading directory is loaded based on PHP's default extension directory. In the Windows environment, please note that the file extension is .dll. When the extension fails to load, not only will this function return false, but an E_WARNING error message will also be generated. Finally, this function is also unavailable in PHP safe mode.

To sum up, in a production environment, we still try not to use the ability to dynamically load extensions. This can be used as a learning material for us. It can be used when you don’t want to load too many extensions at once on your local computer. When you need to test certain functions and require some special extensions, consider using this function for local testing. .

Recommended learning: php video tutorial

The above is the detailed content of How to dynamically view and load PHP extensions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:imooc.com. If there is any infringement, please contact admin@php.cn delete