Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Singleten(dobrze zrozuminy?
daniel1302
post 12.08.2008, 21:27:02
Post #1





Grupa: Zarejestrowani
Postów: 602
Pomógł: 30
Dołączył: 1.08.2007
Skąd: Nowy Sącz

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


  1. <?php
  2. /**********************************************************************
  3. *Autor: daniel1302
  4. *Licencja: by DamnedLand Staff
  5. *Zadanie pliku:
  6. *Zainicjuje istnienie odpowiedniej klasy
  7. **********************************************************************/
  8.  
  9. Class Singleton
  10. {
  11. #Zdefiniuj otwieraną klasę
  12. protected $instance;
  13. #Wykaż wszystkie otwarte klasy
  14. public $openedClass = array();
  15. #Wymagana konfigracja
  16. private $conf = Kernel::config()->framework('framework');
  17.  
  18. /**
  19. *Zainicjuj klase z Frameworka
  20. *
  21. *@return object Wskazana klasa
  22. */
  23. public function instanceFramework($class)
  24. {
  25. $file = $this->conf['frameworkRoot'].'/'.$class.'.php';
  26. return $this->instance($file, $class);
  27. }
  28.  
  29. /**
  30. *Zainicjuj klase z gry
  31. *
  32. *@return object Wskazana klasa
  33. */
  34. public function instanceGame($class)
  35. {
  36. $file = $this->conf['gameRoot'].'/class/Class.'.$class.'.php';
  37. return $this->instance($file, $class);
  38. }
  39.  
  40. /**
  41. *Zapisz otwierane dane oraz zainicjuj klasę
  42. *
  43. *@return object Wskazana klasa
  44. */
  45. protected function instance($file, $class)
  46. {
  47. $this -> openedClass[]['file'] = $file;
  48. $this -> openedClass[]['class'] = $class;
  49. if (!file_exists($file))
  50. return false;
  51. else
  52. {
  53. $this -> instance = @new $class;
  54. if (class_exists($this->instance, false))
  55. {
  56. $this -> openedClass[]['status'] = false;
  57. return false;
  58. }
  59. else
  60. {
  61. $this -> openedClass[]['status'] = true;
  62. return $this -> instance;
  63. }
  64. }
  65. }
  66.  
  67. /**
  68. *Skasuj niepotrzebne
  69. *
  70. *@return null null
  71. */
  72. function __destruct()
  73. {
  74. unset($this->instance);
  75. unset($this->openedClass);
  76. unset($this->conf);
  77. }
  78. }
  79. ?>


Jest to klasa wzorca singleton(ja go tak rozumie czy dobrze?)
Jak go użyje:
do kontrolera dodam
$this -> load = new Singleton;


Jeszcze dla pewności.
Wzorzec singleton odpowiada za inicjacje pojedynczej klasy w pamięci?
Czyli ze jeśli chę otworzyć na chwilę klase to używam go tak?
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: 19.07.2025 - 08:36