Most of the PHP configuration is done by looking at the php.ini file or by querying information from the command line. PHP also has built-in functions to view and manipulate these configuration parameters. For example, we have learned about PHP INI file related operation function analysis. The only function that modifies the aspect is ini_set(). Most of the other functions actually help us with the query. Today, we will explain these functions.

get_defined_constants()

Returns an associative array of all constants whose keys are constant names and whose values are constant values.

define("MY_CONSTANT".1);
print_r(get_defined_constants(true));
// array(
/ /...
// [user] => array(
// [MY_CONSTANT] => 1
/ /)
// )
Copy the code

This function outputs all constants, and since it returns a lot, use…… Means that there are many system or extension defined constants, and the constants we define ourselves in the code will all go into the key name [user].

This function takes one argument, which, when true, makes the function return a multidimensional array of key names classified into the first dimension, constants and their values in the second dimension. By default, false returns a one-dimensional array with constant names as key names and their values as key values.

get_extension_funcs()

This function returns the names of all methods contained in the extension module.

print_r(get_extension_funcs("swoole"));
// Array
/ / (
// [0] => swoole_version
// [1] => swoole_cpu_num
// [2] => swoole_last_error
// [3] => swoole_async_dns_lookup_coro
// [4] => swoole_async_set
// [5] => swoole_coroutine_create
/ /...
// [35] => swoole_timer_clear
// [36] => swoole_timer_clear_all
// )
Copy the code

Its argument is the name of the extension to be queried, and here we’re going to go straight to the native installed Swoole that contains those methods. As you can see, Swoole4.4 contains a total of 37 method functions.

get_loaded_extensions()

This function returns a list of all loaded extension modules.

print_r(get_loaded_extensions());  // php -m
// Array
/ / (
// [0] => Core
// [1] => phpdbg_webhelper
// [2] => date
// [3] => libxml
// [4] => openssl
// [5] => pcre
// [6] => sqlite3
/ /...
// [65] => imagick
// [66] => swoole
// [67] => vld
// [68] => Zend OPcache
// )
Copy the code

Does this function work the same way we use -m on the command line to view the extensions currently installed on the system? Yes, they are the same functionality and both return a list of such extensions installed. In some open source CMS systems, you can use this function to check whether the current installation environment meets requirements.

Get_include_path () and get_included_files ()

The two functions return the configuration of the current include_path, and the list of files included or required.

echo get_include_path(), PHP_EOL; / /. : / usr/local/Cellar/PHP / 7.3.0 / share/PHP/pear
echo ini_get('include_path'), PHP_EOL; / /. : / usr/local/Cellar/PHP / 7.3.0 / share/PHP/pear
Copy the code

Get_include_path () is very simple, it does exactly the same thing as echo ini_get(‘include_path’) to read the configuration value of the include_path in the php.ini file. When PHP does include or require, if it doesn’t have a given path, it looks in the current directory first. If it doesn’t find one, it looks in the include_path. An error is reported if the specified file is still not found. This is where the include_path directory comes in, and of course, we rarely use it in our daily development, so here’s a look at it.

include Dynamically view and load the PHP extension.php;
print_r(get_included_files());
// Array
/ / (
// [0] => /Users/zhangyue/MyDoc/ blog-blog/PHP /202005/source/ PHP
// [1] => /Users/zhangyue/MyDoc/ blogpost /dev-blog/ PHP /202005/source/
// )
Copy the code

Get_included_files () is also a very neat and intuitive function that returns which files are currently loaded. The current runtime file is always first, that is, the function returns at least one of its own file paths. We can try using this function in Laravel or other framework entry files or controllers to print the number of their load files, which will actually help us understand the framework’s load execution.

get_resources()

var_dump(get_resources());
// array(3) {
/ / [1] = >
// resource(1) of type (stream)
/ / [2] = >
// resource(2) of type (stream)
/ / [3] = >
// resource(3) of type (stream)
// }

$fp = fopen('1.txt'.'r');
var_dump(get_resources());
// array(4) {
/ / [1] = >
// resource(1) of type (stream)
/ / [2] = >
// resource(2) of type (stream)
/ / [3] = >
// resource(3) of type (stream)
/ / [5] = >
// resource(5) of type (stream-context)
// }
Copy the code

This function returns the status of the active resource. For example, in the example above, we first print the contents of the function, which has only three pieces of data, and then load a file resource with fopen() to get a resource handle. If you print the contents of this function, you will find an additional resource handle of type stream-context. This function can be used to check if there are any freed resource operations during debugging.

conclusion

Today, I learned a few simple functions. In fact, they are not very common functions, but after learning, I found that many functions can help us tune the system or quickly detect the operating environment when migrating the system. Learning is the most successful learning, let us continue to come on!

Test code:

Github.com/zhangyue050…