Deep understanding of PHP's Require/include sequence recommendation _php Tips

Source: Internet
Author: User
Tags php print

There are also the following questions:

How did the include_path work?

What if there are multiple include_path order?

Under what circumstances does include_path not work?

Today, I will introduce the problem in a comprehensive way, starting with an example.

The directory structure as follows:

 
  
  
  1. Root
  2. 1. PHP
  3. 3. PHP
  4. └subdir
  5. 2. PHP
  6. 3. PHP

In the 1.php:

Copy Code code as follows:

<?php
Ini_set ("Include_path", ".:p ath_to_subdir");
Require ("2.php");
?>

And in the 2.php:
Copy Code code as follows:

<?php
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 you get when you run 1.php in the root directory?
2. What output will you get when you run the 1.php of the top level directory under SubDir?
3. When canceling the current directory path in Include_path (that is, include_path= "Path_to_subdir"), what output will the two problems be?
The Include_path in PHP
When PHP encounters require (_once)/include (_once) 's instructions, it first makes the following judgments:

Copy Code code as follows:

Is the file path you want to include an absolute path?
If it is, it is directly contained and ended.
If not, go to another logic (after multiple calls, the macro expands to enter _php_stream_fopen_with_path) to find this file

Next, in the _php_stream_fopen_with_path, you will make the following judgments:
Copy Code code as follows:

The file path to include is a relative path (in the form of./file,. /dir/file, use "Directory relative path instead")?
If so, skip the include_path logic and directly parse the relative path (then introduce it separately)

Consists of a list of directories to be selected based on include_path, and path to the currently executing file, for example, for the previous examples of the article, which will form a list of the following to be selected
Copy Code code as follows:

".:p Ath_to_subdir:current_script_dir

Then, starting from the head of the selected list, take a path from the list that you want to select from the Default_dir_separator (the environment in this article is ":"), and then append the file name you wish to include to the path, and try. If it is successfully included, it is returned, otherwise the next pending path is resumed.
So far, we have been able to answer the 3 questions I asked you to begin with.
1. Because it is executed in the root directory, the second alternative path for include_path (Path_to_subdir), which contains 2.php in 1.php, finds path_to_subdir/2.php, When 2.php contains 3.php, the current working directory is root, so in the case of 3.php, Include_path's first alternative path is "." The matching file is found under the current working directory, so the resulting output is "root".
2. With 1, only the current path is subdir, so the resulting output is "subdir".
3. Because there is no current path for include_path, so in the root directory when running 2.php contains 3.php, is Path_to_subdir played a role, so whether in root or subdir will get "subdir" output.
And if you empty the include_path in 2.php,
Copy Code code as follows:

<?php
Ini_set ("Include_path", "");
Require ("3.php");
?>

Then it will be current_script_dir, and this time the Current_script_dir is the 2.php path, so it will still 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 directory structure above, except that the 1.php becomes:
Copy Code code as follows:

<?php
Ini_set ("Include_path", "/");
Require ("./subdir/2.php");
?>

2.php has become:
Copy Code code as follows:

<?php
Require ("./3.php");
?>

If executed in the root directory, the search 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 executed under SubDir, the 1.php (php-f) of the upper-level directory. /1.php), will exit unexpectedly because "./subdir/2.php" is not found under SubDir.
PostScript
1. Because of the use of include_path and relative paths, the performance will be related to the number of searches, at worst, if you have 10 include_path, you may retry 11 times to find the file you want to include, so Absolute paths are best used when absolute paths can be used.
2. Because the basedir of the directory relative path is always the current work path, if it is to be used, it needs to be related to the actual deployment path, so the actual use is very little (of course, there are modules to be completed with the help of ChDir).
3. In the modular system design, it is generally within the module to use the absolute path by acquiring the module's deployment path (DirName (__file__), php5.3 later providing the __DIR__ constant).

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.