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