Error trapping config class
| From: | Leonie Price | Date: | Wed, 07 Mar 2007 00:56:07 +0000 |
| Subject: | Error trapping config class | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-26747@lists.php.net to get a copy of this message | ||
I have an ini file containing a number of items:
my.ini.php:
$gs_config['person'] = '2007-02-02 22:57:00';
$gs_config['order'] = '2007-02-02 22:57:00';
I have also created a simple class to manipulate this ini file:
require_once("Config.php");
class gs_config {
var $ini_file = "my.ini.php";
var $config=null;
var $options = array('name' => 'gs_config');
var $root=null;
function gs_config() {
$this->config = new Config();} function read() {
if (!$this->root) {
$this->root =& $this->config->parseConfig($this->ini_file,
"PHPArray", $this->options);
}} function extract($key) {
$this->read();
$item = $this->root->getItem('directive', $key);
return $item->getContent();
}
function save($key, $value) {
$this->read();
$item = $this->root->getItem('directive', $key);
$item->setContent($value);
$this->store();
}
function store() {
$this->config->writeConfig($this->ini_file, "PHPArray", $this->options);} } It works well if all the keys are present. However if I do this for a key that is not present: $c = new gs_config(); print($c->extract('person')); I get this error: Fatal error: Call to a member function getContent() on a non-object in ..\gs_config.php on line 28 What would be the best way to check for the existence of the key and create an empty key if it isn't found?