Home > Article > Backend Development > Be careful about the relative directory of the require_once() function in PHP_PHP Tutorial
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
The code is as follows
|
Copy code
|
||||
require_once("a.php"); require_once("../a.php"); | class B extends A
echo "xx";
http://www.bkjia.com/PHPjc/632201.htmlwww.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...