Home > Article > Backend Development > How to dynamically view extension status in PHP? How to dynamically load extensions?
How to dynamically check the extension status in PHP? How to dynamically load extensions? The following article will introduce you to the method of checking the extension status and loading the extension during dynamic operation. I hope it will be helpful to you!
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.
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.
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 dl () function to dynamically load an extension.
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. .
测试代码: https://github.com/zhangyue0503/dev-blog/blob/master/php/202005/source/%E5%8A%A8%E6%80%81%E6%9F%A5%E7%9C%8B%E5%8F%8A%E5%8A%A0%E8%BD%BDPHP%E6%89%A9%E5%B1%95.php 参考文档: https://www.php.net/manual/zh/function.extension-loaded.php https://www.php.net/manual/zh/function.dl.php
This article is reproduced from: https://juejin.cn/post/6953791854441660424
Author: Hardcore Project Manager
Recommended study: " PHP video tutorial》
The above is the detailed content of How to dynamically view extension status in PHP? How to dynamically load extensions?. For more information, please follow other related articles on the PHP Chinese website!