note 95747 added to function.parse-ini-file

From: Date: Tue, 19 Jan 2010 23:14:52 +0000
Subject: note 95747 added to function.parse-ini-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-165122@lists.php.net to get a copy of this message
I have recently finished an implementacion of a multiple configuration type class. <?php class Configuration { const AUTO = 0; const JSON = 2; const PHP_INI = 4; const XML = 16; static private $CONF_EXT_RELATION = array( 'json' => 2, // JSON 'ini' => 4, // PHP_INI 'xml' => 16 // XML ); static private $instances; private $data; static public function objectToArray($obj) { $arr = (is_object($obj))? get_object_vars($obj) : $obj; foreach ($arr as $key => $val) { $arr[$key] = ((is_array($val)) || (is_object($val)))? self::objectToArray($val) : $val; } return $arr; } private function __construct($file, $type = Configuration::AUTO) { if ($type == self::AUTO) { $type = self::$CONF_EXT_RELATION[pathinfo($file, PATHINFO_EXTENSION)]; } switch($type) { case self::JSON: $this->data = json_decode(file_get_contents($file), true); break; case self::PHP_INI: $this->data = parse_ini_file($file, true); break; case self::XML: $this->data = self::objectToArray(simplexml_load_file($file)); break; } } static public function & getInstance($file, $type = Configuration::AUTO) { if(! isset(self::$instances[$file])) { self::$instances[$file] = new Configuration($file, $type); } return self::$instances[$file]; } public function __get($section) { if ((is_array($this->data)) && (array_key_exists($section, $this->data))) { return $this->data[$section]; } } public function getAvailableSections() { return array_keys($this->data); } } $configuration = Configuration::getInstance(/*configuration filename*/); foreach($configuration->getAvailableSections() as $pos => $sectionName) { var_dump($sectionName); var_dump($configuration->{$sectionName}); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 190.192.21.65 ---- Manual Page -- http://www.php.net/manual/en/function.parse-ini-file.php Edit -- https://master.php.net/note/edit/95747 Del: integrated -- https://master.php.net/note/delete/95747/integrated Del: useless -- https://master.php.net/note/delete/95747/useless Del: bad code -- https://master.php.net/note/delete/95747/bad+code Del: spam -- https://master.php.net/note/delete/95747/spam Del: non-english -- https://master.php.net/note/delete/95747/non-english Del: in docs -- https://master.php.net/note/delete/95747/in+docs Del: other reasons-- https://master.php.net/note/delete/95747 Reject -- https://master.php.net/note/reject/95747 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165122) next »