Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> problem z destruktorem, __destruct() nie ma dostepu do plikow??
bregovic
post 30.07.2004, 13:25:26
Post #1





Grupa: Zarejestrowani
Postów: 562
Pomógł: 15
Dołączył: 8.08.2003
Skąd: Denmark/Odense

Ostrzeżenie: (0%)
-----


Wlasnie skompilowalem php5 i chcialem wyprobowac przyklad singeltonu z ksiazki "Core php Programming 3rd edition". Przyklad wyglada nastepujaco:
  1. <?php
  2. class Configuration
  3. {
  4. static private $instance = NULL;
  5. private $ini_settings;
  6. private $updated = FALSE;
  7. const INI_FILENAME = &#092;"./test.txt\";
  8.  
  9. private function __construct()
  10. {
  11. if(file_exists(self::INI_FILENAME))
  12. {
  13. $this->ini_settings =
  14. parse_ini_file(self::INI_FILENAME);
  15. }
  16. }
  17.  
  18. private function __destruct()
  19. {
  20. //if configuration hasn't changed, no need
  21. //to update it on disk
  22. if(!$this->updated)
  23. {
  24. return;
  25. }
  26.  
  27. //overwrite INI file with the
  28. //version in memory
  29. $fp = fopen(self::INI_FILENAME, 'w+');
  30. if(!$fp)
  31. {
  32. return;
  33. }
  34.  
  35. foreach ($this->ini_settings as $key => $value)
  36. {
  37. fwrite($fp, &#092;"$key = \"$value\\"n\");
  38. }
  39.  
  40. fclose($fp);
  41. }
  42.  
  43. public function getInstance()
  44. {
  45. if(self::$instance == NULL)
  46. {
  47. self::$instance = new Configuration();
  48. }
  49.  
  50. return self::$instance;
  51. }
  52.  
  53. public function get($name)
  54. {
  55. if(isset($this->ini_settings[$name]))
  56. {
  57. return $this->ini_settings[$name];
  58. }
  59. else
  60. {
  61. return(NULL);
  62. }
  63. }
  64.  
  65. public function set($name, $value)
  66. {
  67. //update only if different from what
  68. //we already have
  69. if(!isset($this->ini_settings[$name]) OR
  70. ($this->ini_settings[$name] != $value))
  71. {
  72. $this->ini_settings[$name] = $value;
  73. $this->updated = TRUE;
  74. }
  75. }
  76. }
  77.  
  78. //Test the class
  79. $config = Configuration::getInstance();
  80. $config->set(&#092;"username\", \"leon\");
  81. $config->set(&#092;"password\", \"?\");
  82. print($config->get(&#092;"username\"));
  83. ?>


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


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


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...


--------------------
Prank - for the fun. Mac - for the simplicity. Deviantart - for the kick.
Life is ours, We live it our way -- Metallica
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 02:16