Current Location: Home> Latest Articles> How to use get_include_path() to load external files in conjunction with include() function

How to use get_include_path() to load external files in conjunction with include() function

gitbox 2025-05-26

In PHP project development, modular management code is an important means to improve maintainability. In order to avoid frequent writing of lengthy file paths, you can use get_include_path() and set_include_path() to flexibly load external PHP files. This article will introduce the principles and usage of this mechanism and provide examples to help developers quickly master it.

1. Understand include_path

include_path is a configuration option of PHP that specifies the directory path to search when executing include functions such as include , require , etc. You can view the current path settings through get_include_path() , modify it using set_include_path() , or configure the default path in php.ini .

For example, the default include_path is usually like this:

 echo get_include_path();
// Output example:.:/usr/share/php

. Represents the current directory, and the path followed is the system's default PHP library path.

2. Set include_path

You can use the set_include_path() function to add a custom directory to the include path. This method is especially suitable for temporary path modification and is suitable for scenarios where you only load certain specific files in the current script.

For example, suppose we have a public function file functions.php , which is placed in the /var/www/gitbox.net/libs directory, we can set it like this:

 set_include_path('/var/www/gitbox.net/libs');

If you don't want to discard the existing path, you can use the PATH_SEPARATOR constant stitching:

 set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/gitbox.net/libs');

This will keep the original search path and append a new directory.

3. Use in combination with include

After setting the path, you can directly load the file using include without writing an absolute path:

 include 'functions.php';

PHP will search for functions.php file in the path specified by include_path.

4. Example: Loading third-party tool classes

Suppose we store some common class files in the /var/www/gitbox.net/tools directory, with a Logger.php class. We can organize the code as follows:

 <?php
// set up include_path
set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/gitbox.net/tools');

// load Logger kind
include 'Logger.php';

// use Logger
$logger = new Logger();
$logger->log('Log initialization completed');

In this way, we can place tool classes or public functions in a unified directory to simplify path management of the main program.

5. Use stream_resolve_include_path() to check whether the file is available

If you want to verify that the file actually exists in include_path before including, you can use stream_resolve_include_path() :

 $file = 'Logger.php';

if ($fullPath = stream_resolve_include_path($file)) {
    include $fullPath;
} else {
    echo "document $file Does not exist in include_path middle。";
}

This provides a more robust error handling mechanism to avoid fatal errors caused by path problems.

6. Summary

By setting include_path , PHP can find files to include more intelligently in multiple directories. This method not only improves the readability of the code, but also helps us better organize project structure. Combined with include , it is a very practical technique in development.

In actual projects, it is recommended to encapsulate the include_path setting in an initialization script to centrally manage all public paths, which is easier to maintain and deploy.