PHP数组或函数-在这种情况下哪个更有效

PHP数组或函数-在这种情况下哪个更有效,php,performance,function,global-variables,Php,Performance,Function,Global Variables,使用PHP5.6,并将在未来12-18个月内在此特定应用程序上迁移到7.0 因此,我们有一个相当大的全局配置文件——到目前为止,它保存了将近100个变量(并且每次更新都会得到更多)。正如您所期望的,应用程序中的每个脚本页面都会调用这个配置文件,但并非所有配置值都在所有情况下使用——但为了方便起见,我们将它们都放在同一个文件中 但我认为将值装入函数可能会更有效,但由于我不是PHP语言(或任何语言)的架构师,我不知道使用函数是否更有效、效率更低,或者几乎没有区别 下面是一个示例场景。在配置文件中,我

使用PHP5.6,并将在未来12-18个月内在此特定应用程序上迁移到7.0

因此,我们有一个相当大的全局配置文件——到目前为止,它保存了将近100个变量(并且每次更新都会得到更多)。正如您所期望的,应用程序中的每个脚本页面都会调用这个配置文件,但并非所有配置值都在所有情况下使用——但为了方便起见,我们将它们都放在同一个文件中

但我认为将值装入函数可能会更有效,但由于我不是PHP语言(或任何语言)的架构师,我不知道使用函数是否更有效、效率更低,或者几乎没有区别

下面是一个示例场景。在配置文件中,我们有如下内容:

$g['user']['enable_username_change'] = true;
$g['user']['enable_image_change'] = true;
$g['user']['display'] = "[LASTNAME], [FIRSTNAME]";
$g['user']['sort_by'] = "[LASTNAME]";
$g['user']['default_locale'] = "english";
$g['user']['profile_page'] = file_get_contents('profile_template.html');
if ( $g['user']['enable_username_change'] == true ) {
   // the code to enable it ...
}
function user__getGlobalConfig( $in_param_name ) {
    // DEFINE THE VALUES
    $g['user']['enable_username_change'] = true;
    $g['user']['enable_image_change'] = true;
    $g['user']['display'] = "[LASTNAME], [FIRSTNAME]";
    $g['user']['sort_by'] = "[LASTNAME]";
    $g['user']['default_locale'] = "english";
    $g['user']['profile_page'] = file_get_contents('profile_template.html');

    if ( isset( $g['user'][$in_param_name] == true ) {
        return $g['user'][$in_param_name];
    } else {
        return false;
    }

}
if ( user__getGlobalConfig('enable_username_change') == true ) {
   // the code to enable it ...
}
所有脚本都可以使用这些值,但只有少数脚本需要它们。很明显,我们只是通过这样做来访问它们:

$g['user']['enable_username_change'] = true;
$g['user']['enable_image_change'] = true;
$g['user']['display'] = "[LASTNAME], [FIRSTNAME]";
$g['user']['sort_by'] = "[LASTNAME]";
$g['user']['default_locale'] = "english";
$g['user']['profile_page'] = file_get_contents('profile_template.html');
if ( $g['user']['enable_username_change'] == true ) {
   // the code to enable it ...
}
function user__getGlobalConfig( $in_param_name ) {
    // DEFINE THE VALUES
    $g['user']['enable_username_change'] = true;
    $g['user']['enable_image_change'] = true;
    $g['user']['display'] = "[LASTNAME], [FIRSTNAME]";
    $g['user']['sort_by'] = "[LASTNAME]";
    $g['user']['default_locale'] = "english";
    $g['user']['profile_page'] = file_get_contents('profile_template.html');

    if ( isset( $g['user'][$in_param_name] == true ) {
        return $g['user'][$in_param_name];
    } else {
        return false;
    }

}
if ( user__getGlobalConfig('enable_username_change') == true ) {
   // the code to enable it ...
}
所以我想通过这样做来改变这种工作方式(如果它能提高效率的话):

$g['user']['enable_username_change'] = true;
$g['user']['enable_image_change'] = true;
$g['user']['display'] = "[LASTNAME], [FIRSTNAME]";
$g['user']['sort_by'] = "[LASTNAME]";
$g['user']['default_locale'] = "english";
$g['user']['profile_page'] = file_get_contents('profile_template.html');
if ( $g['user']['enable_username_change'] == true ) {
   // the code to enable it ...
}
function user__getGlobalConfig( $in_param_name ) {
    // DEFINE THE VALUES
    $g['user']['enable_username_change'] = true;
    $g['user']['enable_image_change'] = true;
    $g['user']['display'] = "[LASTNAME], [FIRSTNAME]";
    $g['user']['sort_by'] = "[LASTNAME]";
    $g['user']['default_locale'] = "english";
    $g['user']['profile_page'] = file_get_contents('profile_template.html');

    if ( isset( $g['user'][$in_param_name] == true ) {
        return $g['user'][$in_param_name];
    } else {
        return false;
    }

}
if ( user__getGlobalConfig('enable_username_change') == true ) {
   // the code to enable it ...
}
然后我们会像这样访问它:

$g['user']['enable_username_change'] = true;
$g['user']['enable_image_change'] = true;
$g['user']['display'] = "[LASTNAME], [FIRSTNAME]";
$g['user']['sort_by'] = "[LASTNAME]";
$g['user']['default_locale'] = "english";
$g['user']['profile_page'] = file_get_contents('profile_template.html');
if ( $g['user']['enable_username_change'] == true ) {
   // the code to enable it ...
}
function user__getGlobalConfig( $in_param_name ) {
    // DEFINE THE VALUES
    $g['user']['enable_username_change'] = true;
    $g['user']['enable_image_change'] = true;
    $g['user']['display'] = "[LASTNAME], [FIRSTNAME]";
    $g['user']['sort_by'] = "[LASTNAME]";
    $g['user']['default_locale'] = "english";
    $g['user']['profile_page'] = file_get_contents('profile_template.html');

    if ( isset( $g['user'][$in_param_name] == true ) {
        return $g['user'][$in_param_name];
    } else {
        return false;
    }

}
if ( user__getGlobalConfig('enable_username_change') == true ) {
   // the code to enable it ...
}
因此,似乎只有在调用函数时才会读入file_get_contents()类型的值,我相信这会更有效,但我可能错了。其他正确/错误或简单的基于文本的价值观似乎不会带来很大的效率提升,但我在这里提出——关于为什么一种方法比另一种更有效,是否有任何科学或基于事实的推理


谢谢。

如果使用函数方法,则应使用静态变量缓存设置,对其进行编码,以避免每次都重新创建数组。特别是,您不希望每次查找设置时它都调用
file\u get\u contents()

function user__getGlobalConfig( $in_param_name ) {
    static $g;
    if (!isset($g)) {
        $g = array();
        // DEFINE THE VALUES
        $g['user']['enable_username_change'] = true;
        $g['user']['enable_image_change'] = true;
        $g['user']['display'] = "[LASTNAME], [FIRSTNAME]";
        $g['user']['sort_by'] = "[LASTNAME]";
        $g['user']['default_locale'] = "english";
        $g['user']['profile_page'] = file_get_contents('profile_template.html');
    }
    if ( isset( $g['user'][$in_param_name] ) ){
        return $g['user'][$in_param_name];
    } else {
        return false;
    }
}

你的应用程序目前正在遭受痛苦,还是一时兴起?将设置变量移动到函数实际上不会改变任何东西,你仍然在函数中声明相同的值数组。每次调用函数时,都会重新创建数组。这不一定,但随着配置文件的增长,我们希望尽可能提高效率possible@u_mulder对-但若并没有调用函数,那个又怎样呢?对于那个些不需要访问那个些值的脚本来说,它是否更有效?若并没有调用函数,那个么什么也不会发生。