PHP require/include Sequence _php Tutorial

Source: Internet
Author: User
Tags autoload php print
PHP Tutorial Require/include sequence

In a large Web project, Include_path is fundamental to a modular design (and of course, there are many autoload-based designs that do not affect this article), but precisely because of Include_path, It often leads us to some seemingly "bizarre" problems that are caused by not finding the right files. There are questions like: How does include_path work? What happens if there are multiple include_path orders? Under what circumstances Include_ Path does not work? Today, I will introduce this issue comprehensively, starting with an example. The following directory structure: Root 1.php 3.php subdir 2.php 3.php in 1.php:
Copy the code code as follows:
Ini_set ("Include_path", ".:p ath_to_subdir");
Require ("2.php");
?> and in 2.php:
Copy the code code as follows:
Require ("3.php");
?> and in the root directory 3.php print out "root", in the SubDir directory 3.php print out "subdir"; now, here's my question:
1. What output will I get when I run 1.php in the root directory?
2. What output will I get if I run the 1.php of the previous level directory under SubDir?
3. When canceling the current directory in Include_path path (that is, include_path= "Path_to_subdir"), what is the output of the above two problems?
Include_path in PHP
When PHP encounters the instruction of require (_once)/include (_once), it first makes the following judgments:
Copy the code code as follows:
Is the file path you want to include an absolute path?
If it is, it is included directly and ends.
If not, enter another logic (after multiple calls, the macro expands into _php_stream_fopen_with_path) to find this file next, in _php_stream_fopen_with_path, the following will be judged:
Copy the code code as follows:
The file path to include is a relative path (shaped as./file,.. /dir/file, the following "directory relative path instead")?
If it is, then skip the include_path of the function logic, the direct resolution of the relative path (then separately) will be based on the include_path, and the path of the current execution of the file to form a list of directories to be selected, for example, in the earlier case of the article, will form a list of the following to be selected
Copy the code code as follows:
".:p Ath_to_subdir:current_script_dir then, starting with the head of the list to be selected, take a path from the list to be selected according to Default_dir_separator (": "In the context of this article). Then append the file name you want to include in the path and try it. Returns if successful, otherwise continues with the next selected path.
So far, we have been able to answer the 3 questions I have raised at the beginning.
1. Because it is executed in the root directory, when 2.php is included in the 1.php, Include_path's second selected path plays a role (Path_to_subdir), finds the path_to_subdir/2.php, While the 2.php contains 3.php, the current working directory is root, so in the case of 3.php, Include_path's first choice of path "." Matching files found under (current working directory), so the resulting output is "root".
2. The same 1, except that the current path is subdir, so the resulting output is "subdir".
3. Because there is no current path for include_path, so when the root directory is running when 2.php contains 3.php, it is Path_to_subdir played a role, so both root and SubDir will get "subdir" output.
And if you empty the include_path in 2.php,
Copy the code code as follows:
Ini_set ("Include_path", "');
Require ("3.php");
?> so will be Current_script_dir function, and this time Current_script_dir is the path of 2.php, so still will get "subdir" output.
Directory relative path
In the case of a directory-relative path, the base point of the relative path is always the current working directory.
To illustrate the situation under the directory relative path, let's look at an illustration, or the above directory structure, but 1.php becomes:
Copy the code code as follows:
Ini_set ("Include_path", "/");
Require ("./subdir/2.php");
? >2.php has become:
Copy the code code as follows:
Require ("./3.php");
?> if executed in the root directory, looking for 3.php in 2.php will be found in the relative path of the current directory, so the resulting output is "root", and if it is under SubDir to perform the previous level of the directory 1.php (php-f. /1.php), will exit abnormally because "./subdir/2.php" could not be found under SubDir.
Postscript
1. Because of the use of include_path and relative paths, performance will be related to the number of searches, worst case, if you have 10 include_path, then you may retry 11 times to find the files to include, so, Absolute paths are best used where absolute paths can be used.
2. Because the basedir of the relative path of the directory is always the current working path, if it is to be used, it needs to be related to the actual deployment path, so it is seldom used (of course, there are modules done with chdir).
3. In a modular system design, it is common to use absolute paths within the module by obtaining the module's deployment path (DirName (__file__), php5.3 later providing __dir__ constants).

http://www.bkjia.com/PHPjc/632332.html www.bkjia.com true http://www.bkjia.com/PHPjc/632332.html techarticle PHP tutorial require/include sequence in a large Web project, Include_path is the root of a modular design (of course, there are also many autoload-based designs, this is not the shadow ...

  • Related 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.