Home  >  Article  >  Backend Development  >  Be careful about the relative directory of the require_once() function in PHP_PHP Tutorial

Be careful about the relative directory of the require_once() function in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:55:50883browse

Talked about the problem of using require_once to still tell the class to be redefined. Then I remembered the phenomenon I encountered a few days ago. Let me tell you here. I just remembered the specific reason for the investigation today. If you don't tell me, you will almost forget it. It seems that I do everything without going through my brain. The brain's secondary cache, memory, and even the hard drive are pitifully small. Um.

Suppose there are the following three files, c.php a.php b.php and the corresponding storage directory is: localhost/ localhost/ localhost/demo

 代码如下 复制代码
c.php
require_once("a.php");
require_once("demo/b.php");
B::demo();a.php
class A
{
}

The content of b.php is more interesting, because it inherits CLASS A, so I also introduced a.php into it

 代码如下 复制代码
require_once("../a.php");
class B extends A
{
    public static function demo()
    {
    echo "xx";
    }
}

The system reports an error when executing localhost/c.php. The error message is as follows
Warning: require_once(../a.php) [function.require-once]: failed to open stream: No such file or directory in F:wwwdemob.php on line 2
Fatal error: require_once() [function.require]: Failed opening required '../a.php' (include_path='.;C:php5pear') in F:wwwdemob.php on line 2 However, I was surprised to find that if Remove the require_once statement in b.php and the execution is normal. So there must be too many require_once statements defined? The reason is that Class A was redefined twice? But no. If I only add require_once(‘a.php’); to c.php, it will be correct even if I write it twice. So what’s going on?
The reason is that the directory level defined by b.php is inconsistent with the directory level of the c.php execution file, resulting in two require_once statements in c.php. Make it equivalent to

class B extends A {
The code is as follows
 代码如下 复制代码
require_once("a.php");
require_once("../a.php");
class B extends A
{
    public static function demo()
    {
    echo "xx";
    }
}
B::demo();
Copy code


require_once("a.php"); require_once("../a.php");
Public static function demo() {

echo "xx";

} B::demo(); The reason was found, because in c.php, its relative directory ".." is the upper layer of c.php, resulting in an error that the file cannot be found. Therefore, our conclusion is that in PHP, when using require_once, there are different hierarchical relationships and relative directories must be used with caution.
http://www.bkjia.com/PHPjc/632201.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632201.htmlTechArticle talked about the problem of using require_once to still tell the class to be redefined. Then I remembered the phenomenon I encountered a few days ago. Let me tell you here, I just remembered the specific reason for the investigation today...
Statement:
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