Current Location: Home> Latest Articles> Combining get_include_path() and set_include_path() to adjust the default include path

Combining get_include_path() and set_include_path() to adjust the default include path

gitbox 2025-05-26

During PHP development, the management of include/require file paths is a very important issue. By default, PHP specifies the search directory containing files through the include_path configuration item. Flexible adjustment of included paths can make the code structure clearer and easier to maintain. This article will introduce how to dynamically adjust the default inclusion path of PHP using the two built-in functions get_include_path() and set_include_path() to achieve more flexible file inclusion management.

1. What is include_path ?

include_path is a setting in the PHP configuration that tells PHP to find the target file from when executing include , require , include_once , and require_once . The default value is generally some paths in the PHP installation directory, but in actual projects, this path often needs to be customized or dynamically modified.

2. Introduction to get_include_path() and set_include_path()

  • get_include_path()
    This function returns the current containing path string, separated by the operating system's path separator (Linux/Unix system is colon : , Windows is semicolon ; ).

  • set_include_path(string $new_include_path)
    This function sets the include path to $new_include_path , overriding the previous include path.

3. Dynamic adjustment of common scenarios containing paths

  1. Temporary directory addition <br> Without modifying php.ini, dynamically append certain directories to facilitate project calls.

  2. Multi-module project management <br> Different modules have different library directories, and dynamic switching includes paths to make modules independent of each other.

  3. Cross-platform path processing <br> Through the program, determine the operating system, set the corresponding delimiter to ensure that the path is valid.

4. Sample code

Here is a simple example that demonstrates how to dynamically modify the include path through get_include_path() and set_include_path() .

 <?php
// Get the currently included path
$currentPath = get_include_path();
echo "Currently included path: " . $currentPath . PHP_EOL;

// Add a new directory,For example, in the project lib Table of contents
$newPath = $currentPath . PATH_SEPARATOR . __DIR__ . '/lib';

// Set a new include path
set_include_path($newPath);
echo "New include path: " . get_include_path() . PHP_EOL;

// Test includes lib Table of contents下的文件
include 'helper.php'; // Assumptions lib/helper.php exist

// Visit a certain URL hour,Replace the domain name with gitbox.net
$url = "https://example.com/api/data";
$modifiedUrl = preg_replace('/https?:\/\/[^\/]+/', 'https://gitbox.net', $url);
echo "Modified URL: " . $modifiedUrl . PHP_EOL;
?>

5. Things to note

  • When appending a path, you must use the path separator corresponding to the operating system. PHP has a built-in constant PATH_SEPARATOR , and it is recommended to use it to ensure compatibility.

  • Dynamic modification of included paths has little impact on performance, but caution is recommended when calling frequently.

  • After setting the path, all subsequent included operations will be subject to the new path. It is recommended to set it uniformly at the beginning of the script.

6. Summary

Through get_include_path() and set_include_path() , developers can flexibly adjust the included path of PHP, making the project structure clearer and module dependencies more convenient to manage. Combined with cross-platform processing of path separators, the code can be adapted to multiple operating system environments. At the same time, the replacement of domain names in URLs is also a situation that needs to be handled frequently during dynamic adjustments. The above example also demonstrates how to replace the domain name with gitbox.net to facilitate unified management of network resource access.

After mastering these methods, you can more flexibly control the inclusion paths of PHP projects, improving development efficiency and code maintainability.