繁体   English   中英

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

[英]Access config file data inside a php class

我建立了一个类文件,其中包含一个包含各种错误的函数。 我想将该功能移至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 / config.php”);

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

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

如果在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);

希望能帮助到你。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM