php不返​​回配置文件内容

时间:2012-12-01 11:31:08

标签: php include filepath

public function loadConfig($config)
{
    if(is_file(path('config') . $config . '.php'))
    {
        return include_once path('config') . $config . '.php';
    }

}

我在控制器中加载模型具有相同的功能,一切正常。但我不能包含配置文件,路径是正确的。如果我在回归之前提出

include_once path('config') . $config . '.php';
print_r($config_array);

打印数组值

1 个答案:

答案 0 :(得分:1)

你需要删除“_once”(因为防止第二个包含在这个上下文中没有意义,它适用于类但不适用于配置文件)。此外,您需要在包含的文件中包含“return”语句或返回数组,而不是include函数的返回值:

public function loadConfig($config)
{
    $filename = path('config') . $config . '.php';
    if (is_readable($filename)) {
        include($filename);
        return $config_array;
    }

    // error handling, i.e., throw an exception ...
}

使用“return”语句的解决方案:

配置文件:

$config_array = array( ... );
return $config_array;

使用配置加载程序方法的类:

public function loadConfig($config)
{
    $filename = path('config') . $config . '.php';
    if (is_readable($filename)) {
        return include($filename);
    }

    // error handling, i.e., throw an exception ...
}