Set_include_path (DirName (__file__)) was discovered recently when researching the company code, so a line of code that looked up the data on the web was used to clarify the role of the method.
First, the DirName (__file__) method returns the path to the file where the current code is located.
The Set_include_path function is equivalent to the environment variables of windows. Windows can find the required files from the specified path through the path to the environment variable.
When our project needs to introduce multiple class files, function files, especially when the referenced file path is deep, our code becomes the following:
1<?PHP2 include("xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/test1.php");3 include("xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/test2.php");4 include("xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/test3.php");5 include("xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/test4.php");6 include("xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/test5.php");7?>
Does it feel like writing a lot of redundant code?
If we set the Set_include_path (" xxx/xxx/xxx/xxx/xxx/xxx/ xxx/xxx/xxx/xxx/xxx/ "), then the path in front of our code can be omitted.
1 <? php 2 set_include_path ("xxx/xxx/xxx/xxx/xxx /xxx/xxx/xxx/xxx/xxx/xxx/" 3 include ("test1.php" 4 include ("test2.php" 5 include ("test3.php" 6 require ("test4.php" 7 require ("test5.php" 8 ?
The Set_include_path function can also set multiple path variables to be used under Linux: Delimited, under windows, and delimited. Like what:
1<?PHP2 Define(' A ', ' xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/');3 Define(' B ', ' yyy/yyy/yyy/yyy/yyy/yyy/yyy/yyy/');4 Define(' C ', ' zzz/zzz/zzz/zzz/zzz/zzz/zzz/zzz/');5 6 Set_include_path(A.path_separator. B.path_separator.C);7 8 include("test1.php");9 include("test2.php");Ten include("test3.php"); One require("test4.php"); A require("test5.php"); - -?>
Finally, there are two predefined PHP variables
Path_separator: Path delimiter, include multiple path use, under Win, when you want to include multiple paths, you need to use ";" Separated, but under Linux, ":" is used.
There are also directory_separator: directory separators, on Linux is '/' windows is ' \ '.
PHP file references