At first I thought that I could include the class with ini_get. Actually, it is not. Here I only include the path. If I want to use a class, you must use require, require_once, or include to import the data.
Go to the following link:
PHP shortde_path Analysis
In a large web project, include_path is the root of a modular design (of course, there are also many designs based on autoload, which does not affect the discussion in this article ), however, it is precisely because of include_path that we often encounter seemingly "weird" problems caused by the failure to find the correct file.
You will also have the following questions:
How does include_path work?
What if there are multiple sequence de_path statements?
Under what circumstances does include_path not work?
Today, I will give a comprehensive introduction to this question. Let's start with an example.
The following directory structure:
- Root
- Protocol 1.php
- Ipv3.php
- └ Subdir
- Ipv2.php
- Limit 3.ph
In 1.php:
- <? PHP
- Ini_set ("include_path", ".: path_to_subdir ");
- Require ("2. php ");
- ?> </Span
In 2.php:
- <? PHP
- Require ("3. php ");
- ?> </Span
3. php In the root directory prints "root" and 3. php In the subdir directory prints "subdir ";
Now, my question is:
1. What output will I get when I run 1.php in the root directory?
2. Run 1.php in the upper-level directory under subdir. What output will it get?
3. When you cancel the current directory path (also called include_path = "path_to_subdir") in include_path, what are the output of the above two questions?
Include_path in PHP
When PHP encounters the require (_ once)/include (_ once) command, it first makes the following judgment:
- Is the file path to be included an absolute path?
- If yes, It is included directly and ended.
- If not, go to another logic (after multiple calls, expand the macro and enter _ php_stream_fopen_with_path) to find the file.
Next, the following judgment will be made in _ php_stream_fopen_with_path:
- Is the file path to be included a relative path (such as./file,./DIR/file, which is replaced by "directory relative path ")?
- If yes, skip the role logic of include_path and parse the relative path directly (which will be introduced separately later)
A list of directories to be selected will be formed based on include_path and the path of the current execution file. For example, the following list of directories to be selected will be formed for the example above.
- ".: Path_to_subdir: current_script_dir
Then, start from the header of the list to be selected in sequence. According to default_dir_separator (the environment in this article is ":"), extract a path from the list to be selected, and then append the file name to be included to the path, try. if it is successfully included, return; otherwise, continue to the next path to be selected.
So far, we have been able to answer the three questions I raised at the beginning.
1. because it is executed in the root directory. PHP contains 2. in PHP, the second path to be selected in include_path acts (path_to_subdir) and finds path_to_subdir/2.php, while in 2. PHP contains 3. in PHP, the current working directory is root, so it contains 3. in PHP, the first waiting path of include_path ". "(the current working directory), the matched file is found, so the output is" root ".
2. Same as 1, but the current path is subdir, so the output is "subdir ".
3. because the current path is include_path, 2. PHP contains 3. in PHP, It is path_to_subdir, so both root and subdir will get the "subdir" output.
If you clear include_path in 2.php,
- <? PHP
- Ini_set ("include_path ",");
- Require ("3. php ");
- ?> </Span
It will be current_script_dir, and at this time current_script_dir is the path of 2. php, so we will still get the output of "subdir.
Directory relative path
When the relative directory path is used, the base point of the relative path is always the current working directory.
To illustrate the relative directory path, let's look at a column, or the directory structure above, but 1. php is changed:
- <? PHP
- Ini_set ("include_path", "/");
- Require ("./subdir/2. php ");
- ?> </Span
2. php becomes:
- <? PHP
- Require ("./3. php ");
- ?> </Span
If the command is executed in the root directory, 2. search in PHP 3. PHP will be searched in the relative path of the current directory, so the output is "root". If it is under subdir, execute 1.php (PHP-f .. /1.php), because it cannot be found under subdir ". /subdir/2. PHP "and exit unexpectedly.
Postscript
1. because when using include_path and relative path, the performance will be related to the number of searches. In the worst case, if you have 10 include_path, you may try again for up to 11 times to find the file to be included. Therefore, it is best to use the absolute path when an absolute path is available.
2. because the basedir of the relative directory path is always the current working path, if you want to use it, it must be related to the actual deployment path, so it is rarely used (of course, the chdir module is also used ).
3. in modular system design, the module deployment path (dirname (_ file __), php5.3 and later provide _ DIR _ constants) to use absolute paths.