PHP require_once () function relative directory be careful _php tutorial

Source: Internet
Author: User
Tags php definition
Talked about the problem of using require_once to still tell that the class was redefined. So I think of the phenomenon I encountered the other day. Here and everyone to say, today only think of the specific reasons, if you do not say it is almost forgotten. It seems that everything is not through the brain, the brain's level two cache, and memory, and even hard disk are small poor. Well.

Assuming the following three files, the c.php a.php b.php corresponding storage directory is: Localhost/localhost/localhost/demo

The code is as follows Copy Code
c.php
Require_once ("a.php");
Require_once ("demo/b.php");
B::d emo (); a.php
Class A
{
}

B.php's content is interesting, because it has to inherit CLASS A, so I put a.php into it.

copy code
require_once ("... /a.php ");
Class B extends A
{
public static function demo ()
{
echo "xx";
}
}

Execute localhost/c.php system error, error message 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 err Or:require_once () [function.require]: Failed opening required '. /a.php ' (include_path= '); C:php5pear ') in f:wwwdemob.php on line 2 However, surprised to find that if the b.php inside the require_once statement, the execution of normal, then must be require_once statement definition more? The reason is that class A has been redefined two times? But it won't. If I only add require_once (' a.php ') to the c.php, this statement, even if I write two times is true, what is the matter?
The reason is that the directory hierarchy of the b.php definition and the c.php execution file is inconsistent, resulting in two require_once statements inside the c.php. Make it equal to

The code is as follows Copy Code
Require_once ("a.php");
Require_once (".. /a.php ");
Class B extends A
{
public static function demo ()
{
echo "XX";
}
}
B::d emo ();

The reason was found, because inside the c.php, its relative directory "..." Is the c.php of the previous layer, resulting in files can not find an error.
Therefore, we conclude that in PHP, when using require_once, there are different levels of relationships, and there is relative to the use of the directory must be cautious, careful.

http://www.bkjia.com/PHPjc/632201.html www.bkjia.com true http://www.bkjia.com/PHPjc/632201.html techarticle talked about the problem of using require_once to still tell that the class was redefined. So I think of the phenomenon I encountered the other day. Here and everyone to say, today only think of the specific reasons, ...

  • Related Article

    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.