tho*_*olh 0 php runtime-error undefined
我正在编写一个配置文件解析器,并在我的Config.php文件中有一个名为getVals()的函数,但显然当我在测试中调用它时会抛出"未定义函数"错误.
config.php文件
<?php
require_once '../extlib/pear/Config/Lite.php';
class Config {
private $config;
function __construct($conf) {
$this->config = new Config_Lite();
echo "calling open...<br>";
$this->open($conf);
echo "open done...<br>";
}
function open($cfile) {
if (file_exists($cfile)) {
$this->config->read($cfile);
} else {
file_put_contents($cfile, "");
$this->open($cfile);
}
}
function getVals() {
return $this->config;
}
function setVals($group, $key, $value) {
$this->config->set($group, $key, $value);
}
function save() {
$this->config->save();
}
}
?>
Run Code Online (Sandbox Code Playgroud)
cfgtest.php中的测试类
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once '../util/Config.php';
$cfile = "../../test.cfg";
$cfg = new Config($cfile);
if (is_null($cfg)) {
echo "NULL";
} else {
echo $cfg.getVals();
}
?>
Run Code Online (Sandbox Code Playgroud)
产量
calling open...
open done...
Fatal error: Call to undefined function getVals() in cfgtest.php on line 13
Run Code Online (Sandbox Code Playgroud)
我想知道为什么当已经存在函数时出现未定义的函数错误.
在php中调用方法或对象的成员,使用 - >运算符:
if (is_null($cfg))
{
echo "NULL";
}
else
{
echo $cfg->getVals();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
450 次 |
| 最近记录: |