| View previous topic :: View next topic |
| Author |
Message |
Mungbeans
Joined: 10 Jan 2007 Posts: 30 Location: Sydney, Australia
|
Posted: Tue Mar 06, 2007 1:35 pm Post subject: Error trapping config class |
|
|
I have an ini file containing a number of items:
my.ini.php:
| Code: |
<?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:
| Code: |
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:
| Code: |
$c = new gs_config();
print($c->extract('person'));
|
I get this error:
| Code: | 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? |
|
| Back to top |
|
 |
Mungbeans
Joined: 10 Jan 2007 Posts: 30 Location: Sydney, Australia
|
Posted: Wed Mar 07, 2007 3:17 am Post subject: |
|
|
I think this fixes it:
| Code: |
function extract($key) {
$this->read();
$item = $this->root->getItem('directive', $key);
if ($item) {
return $item->getContent();
} else {
$this->root->createItem('directive', $key, '');
$this->store();
return '';
}
}
|
|
|
| Back to top |
|
 |
Mungbeans
Joined: 10 Jan 2007 Posts: 30 Location: Sydney, Australia
|
Posted: Wed Mar 07, 2007 3:50 am Post subject: |
|
|
In fact it does fix it provided there is already a key in the file of the same type as the one being searched for/created. If (for example) the ini file is empty, I get this error:
| Quote: |
Fatal error: Call to undefined method PEAR_Error::getItem() in ...\gs_config.php on line 27
|
Haven't worked out how to error trap for that yet. |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
Posted: Wed Mar 07, 2007 1:24 pm Post subject: |
|
|
| Mungbeans wrote: | In fact it does fix it provided there is already a key in the file of the same type as the one being searched for/created. If (for example) the ini file is empty, I get this error:
| Quote: |
Fatal error: Call to undefined method PEAR_Error::getItem() in ...\gs_config.php on line 27
|
Haven't worked out how to error trap for that yet. |
| Code: |
if (is_object($this->root)) {
$item = $this->root->getItem('directive', $key);
...
}
|
|
|
| Back to top |
|
 |
Mungbeans
Joined: 10 Jan 2007 Posts: 30 Location: Sydney, Australia
|
Posted: Wed Mar 07, 2007 2:57 pm Post subject: |
|
|
| Unfortunately this doesn't work. Even if there are no items in the file it still creates the $root object, and so I still get the error message. |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
Posted: Wed Mar 07, 2007 3:25 pm Post subject: |
|
|
| Mungbeans wrote: | | Unfortunately this doesn't work. Even if there are no items in the file it still creates the $root object, and so I still get the error message. |
Then do it the usual way:
| Code: |
if (!PEAR::isError($this->root)) {
$item = $this->root->getItem('directive', $key);
...
}
|
|
|
| Back to top |
|
 |
|