Pretty rhythm Dear My future PHP Reference file Tips

Source: Internet
Author: User
We know that there is a concept of package in Java, and in. NET there is a more convenient concept of DLL assembly references, which can be easily referenced in our own class by a collection of objects that are grouped together in a packaged form, but because there is no corresponding concept in PHP, So when it comes to referencing objects defined in other files, the two most common functions PHP programmers use are require_once and include, which allows us to work with objects such as classes defined in other class libraries. But a lot of people use the following code simply to make a file reference when using other files that contain the same directory:

Copy the Code code as follows:


Include (' include.php ');


Of course there is no mistake in this way, but in efficiency it is slightly worse than the following:

Copy the Code code as follows:


Include (Realpath (DirName (_file_)). Directory_separator. ' include.php ');


In this way we may need to enter some more, but relative to the previous kind of need PHP engine to go to include_path in the iteration to find all names of ' include.php ' to find the corresponding object, DirName (__file__) The designation of this absolute path allows the system to quickly locate the appropriate file.
In PHP, the constant __file__ is very similar to the AppDomain.CurrentDomain.BaseDirectory in C #, which returns the absolute path to the file where the current code is executing. The function dirname () returns its parent folder path.
Another way to find out more efficiently and simply to write is the include ('./include.php '), which is equivalent to telling the system to find the ' include.php ' file under the current path.
In a large system we often use another better way, we often add the following code in the routing file or other initialization file:

Copy the Code code as follows:


Define (' App_path ', Realpath (dirname (_file_)));


This is equivalent to adding a global variable to the system to indicate the root of the system, and we can use the following code when we need to refer to a file in a specific path later:

Copy the Code code as follows:


Include (App_path. Directory_separator. ' Models '. ' User.php ');


Hope this small sum can help you!
Author: Sean Zhu
Source: http://jujusharp.cnblogs.com

The above describes the pretty rhythm dear my future php Reference file tips, including pretty rhythm dear my content, I hope to be interested in PHP tutorial friends helpful.

  • 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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.