PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Error trapping config class

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> Configuration
View previous topic :: View next topic  
Author Message
Mungbeans



Joined: 10 Jan 2007
Posts: 30
Location: Sydney, Australia

PostPosted: Tue Mar 06, 2007 1:35 pm    Post subject: Error trapping config class Reply with quote

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
View user's profile Send private message
Mungbeans



Joined: 10 Jan 2007
Posts: 30
Location: Sydney, Australia

PostPosted: Wed Mar 07, 2007 3:17 am    Post subject: Reply with quote

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
View user's profile Send private message
Mungbeans



Joined: 10 Jan 2007
Posts: 30
Location: Sydney, Australia

PostPosted: Wed Mar 07, 2007 3:50 am    Post subject: Reply with quote

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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1003

PostPosted: Wed Mar 07, 2007 1:24 pm    Post subject: Reply with quote

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
View user's profile Send private message
Mungbeans



Joined: 10 Jan 2007
Posts: 30
Location: Sydney, Australia

PostPosted: Wed Mar 07, 2007 2:57 pm    Post subject: Reply with quote

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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1003

PostPosted: Wed Mar 07, 2007 3:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> Configuration All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement

'Actiemonitor' online projectmanagement software