note 92085 added to function.parse-ini-file

From: Date: Wed, 08 Jul 2009 08:40:43 +0000
Subject: note 92085 added to function.parse-ini-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-158052@lists.php.net to get a copy of this message
Here's a simple settings class that parses an ini file for settings. It has a private constructor so that it can only be instantiated through the getInstance method, this means that there can only ever be one settings object at a time. <?php class Settings { private static $instance; private $settings; private function __construct($ini_file) { $this->settings = parse_ini_file($ini_file, true); } public static function getInstance($ini_file) { if(! isset(self::$instance)) { self::$instance = new Settings($ini_file); } return self::$instance; } public function __get($setting) { if(array_key_exists($setting, $this->settings)) { return $this->settings[$setting]; } else { foreach($this->settings as $section) { if(array_key_exists($setting, $section)) { return $section[$setting]; } } } } } ?> settings.ini [General] url = "http://www.somesite.com" [Database] host = localhost username = user password = password db = cms adapter = mysqli Using the class <?php $settings = Settings::getInstance(/*path to settings.ini*/); echo $settings->url; print_r($settings->Database); echo $settings->db; ?> Output would be: http://www.somesite.com Array ( [host] => localhost [username] => user [password] => password [db] => cms [adapter] => mysqli ) cms ---- Server IP: 92.48.74.199 Probable Submitter: 80.6.91.221 ---- Manual Page -- http://www.php.net/manual/en/function.parse-ini-file.php Edit -- https://master.php.net/note/edit/92085 Del: integrated -- https://master.php.net/note/delete/92085/integrated Del: useless -- https://master.php.net/note/delete/92085/useless Del: bad code -- https://master.php.net/note/delete/92085/bad+code Del: spam -- https://master.php.net/note/delete/92085/spam Del: non-english -- https://master.php.net/note/delete/92085/non-english Del: in docs -- https://master.php.net/note/delete/92085/in+docs Del: other reasons-- https://master.php.net/note/delete/92085 Reject -- https://master.php.net/note/reject/92085 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158052) next »