访问php类中的配置文件数据

时间:2012-01-09 13:28:48

标签: php oop configuration

我已经构建了一个类文件,其中包含一个包含各种错误的函数。我想将该函数移动到config.php文件。

如何在config.php内部继续使用该功能?

功能:

private function error($errnum=1000) {
    $data = array(
        '1000' => 'Required parameter is missing',
        '1100' => 'Parameter not recognized',
        '2000' => 'Currency type not recognized',
        '2100' => 'Currency amount must be to 2 decimal places',
        '2200' => 'Currencies cannot be the same',      
        '3000' => 'Service currently unavailable',
        '3100' => 'Error in service'
    );  
    $this->result($data[$errnum], $errnum);
} 

我尝试使用:

require_once( “配置/ config.php中”);

在类文件中,但仍然显示错误

解析错误:语法错误,意外T_PRIVATE

2 个答案:

答案 0 :(得分:2)

如果您在Config.php文件中使用它,则必须删除私有部分。

然后,您必须包含用于显示结果的类实例。或者你必须将$this->result($data[$errnum], $errnum);替换为不属于类的东西。

例如,像这样:

function error($errnum=1000) {
    $data = array(
        '1000' => 'Required parameter is missing',
        '1100' => 'Parameter not recognized',
        '2000' => 'Currency type not recognized',
        '2100' => 'Currency amount must be to 2 decimal places',
        '2200' => 'Currencies cannot be the same',      
        '3000' => 'Service currently unavailable',
        '3100' => 'Error in service'
    );  
    echo "Error: ".$data[$errnum]."(".$errnum.")";
}

error(2000);

希望它有所帮助。

答案 1 :(得分:1)

public,protected和private仅在类内部需要。你的函数不是一个方法,而是一个独立的函数,因此私有在那里是无效的。将其移动到类中或删除关键字。

相关问题