Why does the mainstream PHP framework and CMS take absolute paths rather than relative paths?

Source: Internet
Author: User
In a logical sense, the relative path of the directory is less than the absolute path. Like what.. The/admin/is significantly less than the directory traversed by c:/wamp/www/cms/templates/admin/.

Scenario: When using include (_once), require (_once).

Note: The absolute path referred to here means that the server is using the physical path of the file (also known as the root-relative path), whether it is using "__dir__" or a variable. The relative path referred to here is the directory-relative path (./xxx.php), not the relative path to the file name (xxx.php).

Possibility 1: The use of relative paths is resolved to an absolute path and then executed.

Result: The manual indicates that the relative path is not resolved to an absolute path @jackeychen

The possibility of 2:include_path () has an effect on efficiency.

Result: The manual shows that except for a file name similar to xxx.php, the include path () is no longer used as long as it is a route (but @zhaoyi indicates that the relative path before php5.2 is affected by the includepath, Laruence in a 10 article also mentions that the relative path is affected by include_path (in fact he refers to the relative directory of the file name))

The 1:php manual include function: the included file is searched by the path given by the parameter, and if no directory (only the file name) is given, it isfound in the directory specified by the include path. Ifthe file is not found under include path, the include is finally searched in the directory where the script file is called and in the current working directory. If the file is not found at the end, the include structure emits a warning, unlike require, which emits a fatal error. If a path is defined -whether it is an absolute path (under Windows with a drive letter or \ Start, under Unix/linux) or a relative path to the current directory (in. Or.. Start)--include_path are completely ignored. For example, a file with: /start, the parser looks for the file under the parent directory of the current directory.

attached 2: Refer to the article "in-depth understanding of PHP require/include order" from Laruence.

Reply content:

In a logical sense, the relative path of the directory is less than the absolute path. Like what.. The/admin/is significantly less than the directory traversed by c:/wamp/www/cms/templates/admin/.

Scenario: When using include (_once), require (_once).

Note: The absolute path referred to here means that the server is using the physical path of the file (also known as the root-relative path), whether it is using "__dir__" or a variable. The relative path referred to here is the directory-relative path (./xxx.php), not the relative path to the file name (xxx.php).

Possibility 1: The use of relative paths is resolved to an absolute path and then executed.

Result: The manual indicates that the relative path is not resolved to an absolute path @jackeychen

The possibility of 2:include_path () has an effect on efficiency.

Result: The manual shows that except for a file name similar to xxx.php, the include path () is no longer used as long as it is a route (but @zhaoyi indicates that the relative path before php5.2 is affected by the includepath, Laruence in a 10 article also mentions that the relative path is affected by include_path (in fact he refers to the relative directory of the file name))

The 1:php manual include function: the included file is searched by the path given by the parameter, and if no directory (only the file name) is given, it isfound in the directory specified by the include path. Ifthe file is not found under include path, the include is finally searched in the directory where the script file is called and in the current working directory. If the file is not found at the end, the include structure emits a warning, unlike require, which emits a fatal error. If a path is defined -whether it is an absolute path (under Windows with a drive letter or \ Start, under Unix/linux) or a relative path to the current directory (in. Or.. Start)--include_path are completely ignored. For example, a file with: /start, the parser looks for the file under the parent directory of the current directory.

attached 2: Refer to the article "in-depth understanding of PHP require/include order" from Laruence.

In fact, the main problem is the problem of programming:
dira/a1.php, a2.php
Where a1.php contains the a2.php, the code is as follows: include ' a2.php ';
dirb/b.php
B.php inside to contain dira/a1.php
, and then a1.php is loaded, and then he contains a2.php code that goes wrong.

So these frameworks can only be used for absolute paths.

The framework I see is typically defined as a root in index.php: like this: Define (ROOT, dirname (FILE));

In the internal words to parse the path, just add root, not as you say the absolute path directly (you say: why the mainstream PHP framework and CMS are using absolute path instead of relative path?) )

Another problem, the relative path is to parse the absolute path and then read the directory data, so the parse path is the same.

The main reason is the performance, the absolute path speed is fast.

There are two main factors that affect performance:

Include statement algorithm, although the official website mentions the use of relative paths, ignoring include_path

The included file is searched by the path given by the parameter, and if no directory (only the file name) is given, it is searched according tothe directory specified by the include path. Ifthe file is not found under include path, the include is finally searched in the directory where the script file is called and in the current working directory. If the file is not found at the end, the include structure emits a warning, unlike require, which emits a fatal error.

If a path is defined-whether it is an absolute path (under Windows with a drive letter or \ Start, under Unix/linux) or a relative path to the current directory (in. Or.. Start)--include_path are completely ignored. For example, a file with: /start, the parser looks for the file under the parent directory of the current directory.

php5.2 I tested before or will be affected by Include_path, the latest version is unclear.

Absolute path of the cache such as APC. If you use a relative path, you also turn the absolute path into hash. This layer of conversion is saved with an absolute path.

With absolute paths, you can reduce the time it takes to resolve the OS path

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