Home >Backend Development >PHP Tutorial >pretty rhythm dear my future PHP reference file skills

pretty rhythm dear my future PHP reference file skills

WBOY
WBOYOriginal
2016-07-29 08:42:02938browse

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 in other places, but since there is no corresponding concept in PHP, when needing 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 including 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 the code The code is as follows:


include(realpath(dirname(_FILE_)).DIRECTORY_SEPARATOR.' include.php');


We may need to input more in this way, but compared to the previous method, which requires the PHP engine to iterate through include_path to find all names named 'include.php' to find the corresponding object. Specifying an absolute path like dirname(__FILE__) 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 way that is more efficient and simple 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 the routing file or other initialization files:

Copy the 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 in 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

The above introduces pretty rhythm dear my future PHP file reference techniques, including pretty rhythm dear my future content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn