0

I am writing a config file parser and have a function called getVals() in my Config.php file but apparently when I call it in a test it throws an "Undefined function" error.

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

}

?>

Testing class in 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();
}


?>

Output

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

I would like to know why the undefined function error appears when there's the function there already.

4 Answers 4

6

In php to call a method or a member of an object, use the -> operator:

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

Learn more on PHP Object Oriented Programming on PHP's website.

Sign up to request clarification or add additional context in comments.

Comments

1

Call should be using -> operator

$cfg.getVals(); 

should be

$cfg->getVals();

Comments

1

Use $cfg->getVals(); instead of $cfg.getVals(); Now you are trying to do a concatenation !

Comments

0

Ooops ... missed the '->'. Lol. Good catch for all.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.