PHP uses './include. php' to replace 'include. php' _ php Tutorial

Source: Internet
Author: User
PHP replaces include. php with. include. php. Recently, I learned PHP and liked to compare it with. NET. I found that one of the differences is the reference of objects in other files. We know that java has the concept of a package, while. NE has been learning PHP recently and is habitually fond of comparing it. NET to compare and find that one of the differences is the reference of objects in other files. 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:

Include ('include. php ');
Of course, this method is not wrong, but it is slightly less efficient than the following method:

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:

Define ('app _ path', realpath (dirname (_ FILE _); this is equivalent to adding a global variable to the system to indicate the system root directory, when we need to reference a file in a specific path, we can use the following code:

Include (APP_PATH.DIRECTORY_SEPARATOR. 'models'. 'user. php'); hope this small summary will help you!

Note: PHP 5.3 and later have a new Phar feature. it is said that it has the same concept as java's jar package, but there are not many materials on the internet and it has not been used yet. Find an article about it: http://www.ibm.com/developerworks/cn/opensource/os-php-5.3new4/index.html

Author: Sean Zhu
Source: http://jujusharp.cnblogs.com

Bytes. We know that java has the concept of a package, and...

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.