2012-11-02 24 views
0

我正在寫一個配置文件解析器並在我的Config.php文件中有一個名爲getVals()的函數,但顯然當我在測試中調用它時會引發「未定義的函數」錯誤。當PHP存在未定義的函數時它存在

的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(); 
} 

} 

?> 
在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(); 
} 


?> 

輸出

calling open... 
open done... 
Fatal error: Call to undefined function getVals() in cfgtest.php on line 13 

我想知道爲什麼會出現未定義的函數錯誤時,有功能已經有了。

回答

7

在PHP來調用一個方法或對象的一個​​成員,使用 - >運算符:

if (is_null($cfg)) 
{ 
    echo "NULL"; 
} 
else 
{ 
    echo $cfg->getVals(); 
} 

進一步瞭解PHP面向對象編程上PHP's website

1

呼叫應該使用 - >操作

$cfg.getVals(); 

應該

$cfg->getVals(); 
1

使用$cfg->getVals();而不是$cfg.getVals(); 現在你正在嘗試做的級聯!

0

哎呀......錯過了' - >'。大聲笑。一切都好。

相關問題