get_include_path() is a built-in function in PHP to get the include path of the current script. The included path is a collection of one or more directories. When PHP uses include , require , require_once and other statements to load files, it will look up the target files in the order of including paths.
For example, execute the following code:
echo get_include_path();
The output may be similar:
.:/usr/local/lib/php
This indicates that PHP will first look for files in the current directory ( . ) and then look for files in the /usr/local/lib/php directory.
By rationally configuring and using get_include_path() , developers can flexibly manage file loading paths without having to write specific directories in the code.
require_once() is a language structure in PHP that is used to introduce files. Unlike require() , require_once() checks whether the target file has been loaded, and if so, it will not be loaded again, thus avoiding repeated definition errors.
Example:
require_once 'config.php'; // If it has been loaded before config.php,There will be no repeated loading here
Use require_once() heavily in the project, which can ensure that each file is loaded only once, prevent functions or classes from being repeatedly defined, and ensure the safe execution of the code.
Combining the two, you can write more flexible and efficient file loading code. for example:
// Get the currently included path
$includePath = get_include_path();
// Suppose we need to load a library file lib.php,And the file is located in a directory in the containing path
require_once 'lib.php';
Here, PHP will look up lib.php and load it according to the order of included paths. Use require_once to ensure that even if multiple calls are called, no duplicate loading is loaded.
In actual projects, you can dynamically add directories through set_include_path() :
// Append directory to include path
set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/gitbox.net/libs');
// This way, you can import the library file with just a simple call
require_once 'utils.php';
Here, after adding the custom directory /var/www/gitbox.net/libs to the included path, you only need to write the relative file name to load, simplifying the code and avoiding hard-coded paths.
<?php
// Setting Included Path,Contains custom directories
set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/gitbox.net/includes');
// Loading the core configuration file
require_once 'config.php';
// Loading the database connection class
require_once 'db/Database.php';
// Loading tool function library
require_once 'utils.php';
// Using loaded classes and functions
$db = new Database();
$db->connect();
someUtilityFunction();
?>
In the above code, no matter how many files config.php , Database.php and utils.php are called, they will only be loaded once. By rationally setting the included path, the code is simpler and the management is more efficient.
Priority is required to use require_once() : Ensure that key files are not loaded repeatedly.
Reasonably set the included path : avoid writing absolute paths in the code and improve code portability.
Avoid too many levels and too long paths : Too many paths will increase search overhead and affect performance.
Get_included_files() is available for debugging : view the list of files loaded by the current script to help troubleshoot duplicate loading issues.
By combining get_include_path() and require_once() , you can effectively control file loading, avoid problems caused by duplicate loading, and improve the organization and execution efficiency of the code.