Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> singleton i dziedziczenie
ksiadz
post
Post #1





Grupa: Zarejestrowani
Postów: 39
Pomógł: 0
Dołączył: 24.11.2003

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


mam problem z singletonem przez dziedziczenie a mianowicie napisalem taki kod
  1. <?php
  2.  
  3. class system
  4. {
  5. protected static $instance;
  6.  
  7. protected function __construct()
  8. {}
  9.  
  10. public static function get_instance()
  11. {
  12. if( self::$instance === false )
  13. {
  14. self::$instance = new self();
  15. }
  16.  
  17. return self::$instance;
  18. }
  19. }
  20.  
  21. class innysystem extends system
  22. {
  23. public function main_run()
  24. {
  25. }
  26. }
  27.  
  28. $system = innysystem::get_instance();
  29. $system->main_run();
  30.  
  31. ?>

ale podczas pobierania referencji przez funkcje get_instance() tworzy sie nowy obiekt z tym ze jest to 'system' a nie 'innysystem' wiec jak to najprosciej naprawic by we wszystkich klasach potomnych nie trzebabylo wklepywac tej samej funkcji get_instance() ?
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 23.08.2025 - 18:13