The correct usage of the require () file contained in PHP

Source: Internet
Author: User
Tags php framework

Before looking at some PHP framework source code, it is very strange when the file contains, will use DirName (__file__) to cobble together the file path, do not know what the benefits of doing so, and then finally found the reason.

Let's look at a simple example:

There are a,b,c of three PHP files. A.php in the site root directory, b.php under the B folder--b/b.php,c.php under the C folder--c/c.php. Some confusion? Look at the picture at a glance:

Both a.php and b.php contain c.php, and the final c.php contains a php file--d/d.php under the D folder.

Let's look at a.php first:

<?php         $file _name = ' a.php ';        echo "This is a.php";    echo "

Very simple code, after the printout, contains the c/c.php, and then we need to look at c/c.php:

<?php     $c _file_name = ' c.php ';        Echo ' This is c.php, was required by '. $file _name;    echo "

PrintOut "This is c.php, was required by a.php", $file the _name is a variable defined in a.php. In the end, it contains the d.php. Because the D folder is on the previous level of the current c.php file, it is common sense that we will certainly write the path as ". /d/d.php ". But unfortunately, it will be an error. The reason is that in the contained files such as c.php, and then to include other files, the path is relative to the outermost parent file, that is, relative to the a.php, it can be understood that because you are included in me, so you have to me. Looks very iffy, the principle is actually very simple: you can put require (' c/c.php '); As the code in the c/c.php file, so our a.php looks like this:

<?php     $file _name = ' a.php ';        echo "This is a.php";    echo "

To this, you can see that we want to include the d/d.php file, just the path is not wrong? Because, now in the a.php code, we are relative to the a.php file, of course, the path should be require (' d/d.php '); It's right. We modify the code as follows:

<?php     $file _name = ' a.php ';        echo "This is a.php";    echo "

At this point, you have not realized the meaning, need to look down, we look at b/b.php:

<?php     $file _name = ' b.php ';        echo "This is b.php";    echo "

No need to explain it, no problem, but when you put require ('. /c/c.php '); Replace the code inside the c/c.php, you will find the problem, notice, we just modified the code in c/c.php, put require ('. /d/d.php '); Changed to require (' d/d.php '); Look at the following code, including:

<?php     $file _name = ' b.php ';        echo "This is b.php";    echo "

So, as opposed to b/b.php, require (' d/d.php '); The path is wrong and should be require ('. /d/d.php '); Just right. You go back to modify the c/c.php in the Require path, but the wrong ah, you changed, b/b.php can run normally, but a/a.php and not, is not, they share c/c.php, holding a whole body, how to do it.

This time, we go back to the beginning of the article mentioned dirname (__file__), this is a good thing, can completely solve the above problems. With it, you don't have to worry about which file contains your file, under which path, and you don't have to worry about the level at which the parent file is located, because DirName (__file__) can specify the path relative to the current file. In other words, we need to change the require path in our c/c.php to:

<?php     $c _file_name = ' c.php ';        Echo ' This is c.php, was required by '. $file _name;    echo "

Here, we just need to c/c.php as a reference, relative to it, d/d.php on the previous layer. In this way, there is only one standard, that is, to me, whether you contain me, or he contains me, I am only in my own right, I want to include only the documents for myself.

For DirName (__file__) do not understand the same repair, please Google, very simple.

Well, PHP technology to share to this end, there are any questions or errors, please leave a message. In other words, this is my first standard technical blog post. The first article is hydrology, the second is quasi-technology, and today finally wrote a technical, European also.

The correct usage of the require () file contained in PHP

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.