PHP shortde_path usage

Source: Internet
Author: User
Tags constant php tutorial relative require

Ini_set ('include _ path', CAKE_CORE_INCLUDE_PATH. PATH_SEPARATOR. ROOT. DS. APP_DIR. DS. PATH_SEPARATOR. ini_get ('include _ path '));
We can see that this program dynamically modifies include_path. However, cake adds CAKE_CORE_INCLUDE_PATH and APP_DIR to include_path, and preferentially finds inclusion programs in these two directories.
Note that the PATH_SEPARATOR variable is used here. This code can be used in both windows and linux.

Inspired by this, we can dynamically add some include directories as needed. For example, we have many libs: lib1, lib2, lib3, and so on. We don't have to add these libs to include_path because they may conflict.
You can create an inc_dir and add this directory to include_path. In inc_dir, create inc_path1.php tutorial inc_path2.php inc_path3.php
Write separately
<? Php
Ini_set ('include _ path', ini_get ('include _ path'). PATH_SEPARATOR. $ dirToLib1 );
<? Php
Ini_set ('include _ path', ini_get ('include _ path'). PATH_SEPARATOR. $ dirToLib2 );
<? Php
Ini_set ('include _ path', ini_get ('include _ path'). PATH_SEPARATOR. $ dirToLib3 );

When writing a program, for example, using lib2 functions. php
You can write it like this.
<? Php
Require 'Inc _ path2.php ';
Require 'functions. Php ';

 

When the include (), require (), and fopen_with_path () functions are used to find files. without setting include_path, when these functions open files, the web root directory is used by default. after setting include_path, these php functions will first search for them under the specified include_path directory.

The principle is similar to the environment variables of the window system. When the window runs the cmd command, the system will search for the existence of these commands in the environment variables it sets after some cmd commands are entered, if yes, you can execute it.


2. Configure include_path
Method 1:

Modify the include_path entry in the php. Ini file.

Export de_path =.:/usr/local/lib/php:./include
Method 2:

Use the ini_set method.
Ini_set ("include_path", "..: ../:./include: ../include ");

3. Note
Zendframework include setting index. php

Copy the code as follows:
Set_include_path ('.'. PATH_SEPARATOR. '../library /'
. PATH_SEPARATOR. './application/models /'
. PATH_SEPARATOR. './application/lib /'
. PATH_SEPARATOR.get_include_path ());

PATH_SEPARATOR is a constant. It is a ":" number in Linux and a ";" number in Windows.

Therefore, it is best to use the constant PATH_SEPARATOR in programming; otherwise, an error will occur if the system is transplanted from linux to win or vice versa!
Get_include_path gets the existing environment variables, and the preceding setting is the new system include

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.