We know that java has the concept of package, while .NET has the more convenient concept of DLL assembly reference. Through these collections of objects combined in packaged form, we can easily reference other objects in our own classes. Classes or other objects defined elsewhere, but since there is no corresponding concept in PHP, when it is necessary to reference objects defined in other files, the two most commonly used functions by PHP programmers are require_once and include. Through these two functions , we can use objects such as classes defined in other class libraries. But many people simply use the following code to reference files when using other files in the same directory:
Copy the code The code is as follows:
include('include.php');
Of course there is nothing wrong with this method, but it is slightly less efficient than the following method:
Copy code The code is as follows:
include(realpath(dirname(_FILE_)).DIRECTORY_SEPARATOR.'include.php');
In this way we may need to input more, but compared to the previous method which requires the PHP engine to iterate through include_path to find all the names named 'include.php' to find the corresponding object, dirname( __FILE__) This absolute path specification will allow the system to quickly locate the corresponding file.
The constant __FILE__ in PHP is actually very similar to AppDomain.CurrentDomain.BaseDirectory in C#. It returns the absolute path of the file where the code currently being executed is located. The function dirname() returns its parent folder path.
Another more efficient and simple way 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 way. We often add the following code to routing files or other initialization files:
Copy code The 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 under a specific path later, we can use the following code:
Copy the code The code is as follows:
include(APP_PATH.DIRECTORY_SEPARATOR.'models'.'User.php');
Hope this little summary can be helpful to you!
Author: Sean Zhu
Source: http://jujusharp.cnblogs.com
http://www.bkjia.com/PHPjc/321396.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321396.htmlTechArticleWe know that there is the concept of package in java, and there is a more convenient DLL assembly reference in .NET Concept, through these collections of objects combined in a packaged form, we can easily...