<?php class Configuration { private $ini_settings; private $updated = FALSE; const INI_FILENAME = \"./test.txt\"; private function __construct() { { $this->ini_settings = } } private function __destruct() { //if configuration hasn't changed, no need //to update it on disk if(!$this->updated) { return; } //overwrite INI file with the //version in memory if(!$fp) { return; } foreach ($this->ini_settings as $key => $value) { } } public function getInstance() { if(self::$instance == NULL) { self::$instance = new Configuration(); } return self::$instance; } public function get($name) { { return $this->ini_settings[$name]; } else { return(NULL); } } public function set($name, $value) { //update only if different from what //we already have ($this->ini_settings[$name] != $value)) { $this->ini_settings[$name] = $value; $this->updated = TRUE; } } } //Test the class $config = Configuration::getInstance(); $config->set(\"username\", \"leon\"); $config->set(\"password\", \"?\"); ?>
Powyzszy skrypt ma w zalorzeniu wczytac plik i zparsowac go funkcja parse_ini_file" title="Zobacz w manualu PHP" target="_manual. nastepnie uzywajac method get i set mozemy operowac na konfiguracji - ktora powinna zostac zapisana dzieki metodzie __destruct().
Pierwszy problem to prywatnosc destruktora - otrzymuje nastepujacego errora:
Kod
leon
Warning: Call to private Configuration::__destruct() from context '' during shutdown ignored in Unknown on line 0
Warning: Call to private Configuration::__destruct() from context '' during shutdown ignored in Unknown on line 0
Drugi problem pojawia sie gdy usune 'private' sprzed __destruct(). Otrzymuje nastepujacy output:
Kod
leon
Warning: fopen(./test.txt) [function.fopen]: failed to open stream: Permission denied in /home/brego/www/patterns/singelton.php on line 57
Warning: fopen(./test.txt) [function.fopen]: failed to open stream: Permission denied in /home/brego/www/patterns/singelton.php on line 57
Oczywiscie skrypt ma 777, katalog tez. Ponadto gdy przed definicja klasy otworze plik i cos do niego zapisze to dziala...
Czy ktos cos wie, jakoby destruktory nie mialy dostepu do funkcji fopen" title="Zobacz w manualu PHP" target="_manual (?!?) - czy moglby ktos sprobowac zapuscic ten kod na swojej instalacji php5 i sprzawdzic czy dziala?
Dzieki za pomoc...