.ini file * Sections include: * - Layout * * @var array */ public $properties; /** * Sign properties to detect any change * * @var string */ protected $hash; /** * Obtain the file name of the configuration * file, without the directory * * Why static ? Because it is instance-independent * * @return string */ static protected function getConfigFileName() { /** * Why not just use basename ? Because in PHP-GTK * the PHP filename extension can be any .php*, */ $name = $_SERVER['SCRIPT_NAME']; $name = pathinfo($name, PATHINFO_FILENAME); $name .= '.ini'; return $name; } /** * Auto-load configuration * * @return void */ function __construct() { $name = self::getConfigFileName(); $this->properties = file_exists($name) ? parse_ini_file($name, TRUE) : NULL; $this->hash = md5(var_export($this->properties, TRUE)); } /** * Auto-save configuration * * @return void */ function __destruct() { /** * No need to save if properties haven't been changed */ if ($this->hash == md5(var_export($this->properties, TRUE))) { return; } else { echo "Saving application settings\n"; } $name = self::getConfigFileName(); $s = ''; foreach ($this->properties as $sectionKey => $sectionValue) { $s .= "[$sectionKey]" . PHP_EOL; foreach ($sectionValue as $key => $value) { $s .= $key . ' = '; if (is_bool($value)) { $s .= $value ? 'TRUE' : 'FALSE'; } elseif (is_numeric($value)) { $s .= $value; } elseif (is_string($value)) { $s .= '"' . $value . '"'; } else { die('Unsupported property type for ' . "$sectionKey/$key: " . gettype($value) ); } $s .= PHP_EOL; } } file_put_contents($name, $s); } }