In-depth discussion of the use of require_once in functions elegant configuration file definition method recommended _php Instance

Source: Internet
Author: User
background

Many people in the project prefer to use arrays in configuration files to configure individual configuration items, such as hierarchical configuration level.config.php:
Copy the Code code as follows:
<?php
$g _levelconfig = Array (
' 1 ' = ' novice ',
' 2 ' = ' advanced ',
);

Because different modules of the project often call each other's methods, there is a case of repeating a file, in order to avoid errors, people generally use require_one, and often include files in the function, such as:
Copy the Code code as follows:
function Getnamebyleval ($level) {
$level = Intval ($level);
Require_once config_path. ' level.config.php ';
if (!isset ($g _levelconfig[$level])) {
return false;
}else{
return $g _levelconfig[$level];
}
}

Problem

So what's wrong with that? First look at the output of the following code, level.config.php is the configuration file mentioned above
Copy the Code code as follows:
<?php
function Getnamebyleval ($level) {
$level = Intval ($level);
Require_once ' level.config.php ';
if (!isset ($g _levelconfig[$level])) {
return false;
}else{
return $g _levelconfig[$level];
}
}
Var_dump (Getnamebyleval (1));
Var_dump (Getnamebyleval (2));

The output is:
Copy the Code code as follows:
String (6) "Novice"
BOOL (FALSE)

Many people find it strange why the second output is false, which is actually very simple:

Require_once contains only one file, and if the file is already included, it will not be included again.

1. The first execution of Getnamebyleval (1) because it did not contain the level.config.php configuration file, so this time will contain level.config.php files and compiled, all functions have $g_levelconfig variables;

2. When Getnamebyleval (1) is executed for the second time, because the level.config.php configuration file was previously included, this time it is no longer included, so there is no $g_levelconfig variable, which naturally returns false;

Solutions

1. Globally acting on containment, referencing in function
Copy the Code code as follows:
<?php
Require_once ' level.config.php ';//new Code
function Getnamebyleval ($level) {
New code for global $g _levelconfig;//
$level = Intval ($level);
if (!isset ($g _levelconfig[$level])) {
return false;
}else{
return $g _levelconfig[$level];
}
}
Var_dump (Getnamebyleval (1));
Var_dump (Getnamebyleval (2));
In this case, regardless of using the Getnamebyleval function, the level.config.php configuration file should be included in, a little less cost-effective.

2. Include, apply in functions
Copy the Code code as follows:
<?php
function Getnamebyleval ($level) {
$level = Intval ($level);
New code for global $g _levelconfig;//
Require_once ' level.config.php ';
if (!isset ($g _levelconfig[$level])) {
return false;
}else{
return $g _levelconfig[$level];
}
}
Var_dump (Getnamebyleval (1));
Var_dump (Getnamebyleval (2));
It feels so untidy and beautiful.

3. configuration file using static class
Copy the Code code as follows:
<?php
Class levelconfig{
public static $level = Array (
' 1 ' = ' novice ',
' 2 ' = ' advanced ',
);
}

When using the

Copy the Code code as follows:
function Getnamebyleval ($level) {
$level = Intval ($level);
Require_once ' level.config.php ';
if (!isset (levelconfig:: $level [$level])) {
return false;
}else{
Return Levelconfig:: $level [$level];
}
}

I personally highly recommend this way to define configuration files that are elegant and not easy to overwrite variables.

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