Ini File Parsing Class

If you find this code useful and would like to encourage me to post more goodies here, you could buy me something from my wish lists at Amazon.com or at MusicDirect.com

The documentation

The code:


<?php
/**
*  @author  Charles Reace (www.charles-reace.com)
*  @version 1.0
*  @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
*  Read data from ini file and create a data array or object
*
*  @link http://www.php.net/parse_ini_file See the parse_ini_file() documentation for info on the ini file syntax
*/
class IniFile
{
   
/**
   *  @var string relative or absolute path to the ini file
   */
   
private $iniFilePath '';
   
   
/**
   *  Constructor
   *
   *  @param string $path (optional) path to ini file
   */
   
public function __construct($path '')
   {
      if(
$path != '')
      {
         
$this->setIniFilePath($path);
      }
   }
   
   
/**
   *  Set the ini file path
   *
   *  Throws exception on invalid path
   *
   *  @param string $path path to the ini file
   *  @return boolean
   */
   
public function setIniFilePath($path)
   {
      if(
is_readable($path))
      {
         
$this->iniFilePath $path;
         return(
TRUE);
      }
      else
      {
         throw new 
Exception("$path does not exist or is not readable.");
      }
   }
   
   
/**
   *  parse ini file and return data as an object
   *
   *  throws exception if path not set
   *
   *  @return object
   */
   
public function getIniDataObj()
   {
      if(empty(
$this->iniFilePath))
      {
         throw new 
Exception("No ini file path defined");
      }
      
$dataArray parse_ini_file($this->iniFilePathTRUE);
      
$dataObj = new stdClass();
      foreach(
$dataArray as $key => $value)
      {
         if(
is_array($value))
         {
            foreach(
$value as $key2 => $value2)
            {
               
$dataObj->$key->$key2 $value2;
            }
         }
         else
         {
            
$dataObj->$key $value;
         }
      }
      return(
$dataObj);
   }
   
   
/**
   *  parse ini file and return data as an array
   *
   *  throws exception if path not set
   *
   *  @return array
   */
   
public function getIniDataArray()
   {
      if(empty(
$this->iniFilePath))
      {
         throw new 
Exception("No ini file path defined");
      }
      return(
parse_ini_file($this->iniFilePathTRUE));
   }
}