PHP uses './include.php' to replace 'include.php'_PHP tutorial

WBOY
Release: 2016-07-21 14:54:46
Original
796 people have browsed it

I am learning PHP recently, and I like to compare it with .NET. I found that one of the differences is the issue of referencing objects in other files. 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 when many people include other files in the same directory, they simply use the following code to reference files:

include('include.php');
Of course there is nothing wrong with this method. It's just that it's slightly less efficient than the following method:

include(realpath(dirname(_FILE_)).DIRECTORY_SEPARATOR.'include.php');
In this way we may need to enter There are more, but compared to the previous method that requires the PHP engine to iterate through include_path to find all the names named 'include.php' to find the corresponding object, the specification of an absolute path such as dirname (__FILE__) will allow the system to quickly locate the Corresponding documents.

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:

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:

include(APP_PATH.DIRECTORY_SEPARATOR. 'models'.'User.php'); I hope this small summary can be helpful to you!

Note: I just discovered that there is a new Phar feature after PHP 5.3. It is said that it has the same concept as Java’s jar package, but there is not much information online and I haven’t actually used it yet. Found an article introducing it: http://www.ibm.com/developerworks/cn/opensource/os-php-5.3new4/index.html

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364539.htmlTechArticleI am learning PHP recently, and I habitually like to compare it with .NET. I found that one of the differences is the difference between it and .NET. Reference issues with objects in files. We know that there is the concept of package in java, and .NE...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template