PHPrequire/include sequence explanation

Source: Internet
Author: User
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 ), but it is because of include_path ,... 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. then we have the following question: 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 issue. let's start with an example. the following directory structure:

1. php 2.php 3.php

3. php in 1. php:

 

In 2.php:

 

3. php in the root directory prints "root" and 3. php in the subdir directory prints "subdir". now, my problem 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 and enter _ php_stream_fopen_with_path) to search for this file. Next, in _ php_stream_fopen_with_path, make the following judgment:

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 directly parse the relative path (described separately later) to form a list of directories to be selected based on include_path and the path of the current execution file, for example, for the example above, a list to be selected will be formed as follows: path_to_subdir: current_script_dir, and then start from the header of the list to be selected in sequence, according to default_dir_separator (the environment in this article is ":") retrieve a path from the list to be selected, append the file name to be included to the path, and try again. 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) will find the matched file, 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 output of "subdir. and if in 2. clear include_path in php:

 

So it will be current_script_dir, and at this time current_script_dir is the path of 2. php, so it 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 path of the Directory, let's look at a column, or the above directory structure, but 1. php becomes:

 

2. php becomes:

 

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 ". /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.


Article address:

Reprint at will ^ please include the address of this article!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.