Current Location: Home> Latest Articles> Use get_include_path() to implement cross-directory file import with set_include_path()

Use get_include_path() to implement cross-directory file import with set_include_path()

gitbox 2025-05-26

In PHP development, the project structure usually contains multiple subdirectories, and the introduction and management of files become complicated. To avoid introducing files using a large number of relative paths, PHP provides two very practical functions get_include_path() and set_include_path() , which can help us implement file import and management across directories.

This article will introduce the functions and usages of these two functions in detail, and use examples to illustrate how to efficiently manage cross-directory file introduction.

1. The concept of include_path

include_path is a configuration option of PHP, which specifies the path list of the paths that PHP searches for files when executing include , require and other statements. Setting a reasonable include_path can greatly simplify the code when you don't want to write complex relative paths, or when the same file is referenced in multiple places.

2. The functions of get_include_path() and set_include_path()

  • get_include_path()
    Returns the include_path configuration value of the current PHP runtime.

  • set_include_path(string $new_include_path)
    Set the include_path of the current PHP runtime and replace it with the new path string.

3. Use examples

Suppose our project directory structure is as follows:

 /project
    /libs
        helper.php
    /modules
        moduleA.php
    index.php

where helper.php is some public functions, moduleA.php needs to refer to helper.php , and index.php is the entry file.

If you use a relative path reference directly:

 // moduleA.php
include '../libs/helper.php';

When the file path changes, the reference path must also be modified, which is more cumbersome.

1. Set include_path

In index.php , we can set include_path to let PHP automatically find files in the specified directory:

 <?php
// index.php
// Get the current one include_path
$current_path = get_include_path();

// New /libs Directory to include_path middle
$new_path = $current_path . PATH_SEPARATOR . __DIR__ . '/libs';

// Set up a new one include_path
set_include_path($new_path);

// After that, you can directly moduleA.php middle include 'helper.php',No need to write relative paths
?>

2. Import directly into the module file

 <?php
// moduleA.php
include 'helper.php';  // PHP Will be here include_path middle寻找 helper.php
?>

3. Actual testing

Write a simple function in helper.php :

 <?php
// helper.php
function greet($name) {
    return "Hello, $name!";
}
?>

In moduleA.php :

 <?php
include 'helper.php';
echo greet('World');
?>

Run index.php , PHP will find helper.php in the /libs directory because it sets include_path and successfully call it.

4. Things to note

  1. Multiple path separators <br> Use semicolon ( ; ) under Windows and colon ( : ) for Linux/Unix, but the PHP constant PATH_SEPARATOR will automatically adapt, and it is recommended to use it.

  2. Relative path and absolute path <br> It is recommended to use absolute paths (such as __DIR__ . '/libs' ) to avoid path resolution errors.

  3. Dynamic settings vs php.ini configuration <br> You can also configure include_path directly in php.ini , but the dynamic settings in the project are more flexible.

  4. Safety considerations <br> Avoid adding untrusted paths to include_path to prevent code from being injected or containing malicious files.

5. Summary

  • Use get_include_path() to get the current search path.

  • Add a directory to include_path through set_include_path() to implement cross-directory file introduction.

  • Using include or require , there is no need to write complex relative paths, and the code is clearer and easier to maintain.

  • Use it in conjunction with absolute paths to ensure that the file can be found accurately.

Reference link