PHP: replace 'include. php' with './include. php'

Source: Internet
Author: User

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. Net has a more convenient DLL.ProgramThe concept of set reference. Through these object sets that are combined in the form of packaging, we can easily reference classes or other objects defined elsewhere in our own classes, however, because PHP does not have a corresponding concept, 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 followingCodeFile Reference:

 
  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 engineInclude_pathIn iterative search for all objects whose names are 'include. php', the absolute path dirname (_ file _) allows the system to quickly locate the corresponding file.

Constants in PHP_ File __In fact, it 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 isInclude ('./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 point out 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 ');

I 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. FindArticle: Http://www.ibm.com/developerworks/cn/opensource/os-php-5.3new4/index.html

 

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.