Problems with absolute paths in require: constant redefinition, variable overwrite, re-invocation increase overhead

Source: Internet
Author: User
The problem with absolute paths in require: constant redefinition, variable overrides, and re-invocation increase overhead.
Because the relative path refers to the total problem, Google uses the absolute path under reference.
Three methods have been tried in absolute path use.
1, define the path constant, re-use path for require operation.
Here, if the Requrie page of the main page has the same define Paht constant, the error repeats defining the constant path.
2, the $path variable, such as the following code, redefine the variable caused by the value of the overwrite, may lead to other related require path error.
The temporary rejection method resets the $path variable as follows, but it feels silly.
3, do not use the Define variable to define the path path, directly require each time the path is reconstructed, such as
Require DirName (__file__). ' File_name.php ';
But it makes people feel stupid ... and repeated calls to dirname increased overhead is uninteresting ... But in the wrong way, at least not wrong.
4, other No, how to better deal with the absolute path of the require, to you for advice.

PHP Code
  
   
  
   
  
   


------Solution--------------------
Unify the rules. The definition of a path constant is not allowed.
Or you can use defined to make judgments. If it is defined, it is no longer defined.
------Solution--------------------
Defines the path constant, which is typically used for single-entry applications
Since the entry program is always run first, there is no problem in defining it here.

For multi-entry applications, the path constant is defined in the Common environment settings file (e.g. config.php)
To prevent problems during multiple loads, it is generally necessary to determine

if (defined (' PATH ')) return;
Define (' PATH ', DirName (__file__));
Load additional files or define code for public classes and functions

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