We know that java has the concept of a package, while. NET has a more convenient DLL Assembly reference concept, through these together in the form of packaging object set, we can easily reference classes or other objects defined elsewhere in our own classes, but because PHP does not have the corresponding concept, so when you need to reference objects defined in other files, the most common two functions for PHP programmers are require_once and include. Through these two functions, we can use classes and other objects defined in other class libraries. However, many users simply use the following code to reference other files in the same directory:
Copy codeThe Code is as follows:
Include ('include. php ');
Of course, this method is not wrong, but it is slightly less efficient than the following method:
Copy codeThe Code is as follows:
Include (realpath (dirname (_ FILE _). DIRECTORY_SEPARATOR. 'include. php ');
In this way, we may need to enter more information, but compared with the previous method, we need the PHP engine to iterate through de_path to find all the names 'include. php' can find the corresponding object. The absolute path dirname (_ FILE _) allows the system to quickly locate the corresponding FILE.
In PHP, constant _ FILE _ is very similar to AppDomain. CurrentDomain. BaseDirectory in C #. It returns the absolute path of the FILE where the code being executed is located. The dirname () function returns the path of its parent folder.
Another method that is more efficient in searching and easy to write is include ('./include. php'), which is equivalent to telling the system to find the 'include. php' file in the current path.
In large systems, we often use another better method. We often add the following code to the routing file or other initialization files:
Copy codeThe Code is as follows:
Define ('app _ path', realpath (dirname (_ FILE _)));
This is equivalent to adding a global variable to the system to point out the system root directory. When we need to reference a file in a specific path, we can use the following code:
Copy codeThe Code is as follows:
Include (APP_PATH.DIRECTORY_SEPARATOR. 'models'. 'user. php ');
I hope this small summary will help you!
Author: Sean Zhu
Source: http://jujusharp.cnblogs.com