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
 
Start new topic
Odpowiedzi
ksiadz
post
Post #2





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

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


jedyne co mi sie udalo uzyskac to tylko to
  1. <?php
  2.  
  3. class system
  4. {
  5. protected static $instance = array();
  6.  
  7. protected function __construct()
  8. {}
  9.  
  10. public static function get_instance( $classname=__CLASS__ )
  11. {
  12. if( ! isset( self::$instance[ $classname ] ) )
  13. {
  14. self::$instance[ $classname ] = new $classname();
  15. }
  16.  
  17. return self::$instance[ $classname ];
  18. }
  19. }
  20.  
  21. class innysystem extends system
  22. {
  23. public static function get_instance()
  24. {
  25. return parent::get_instance( __CLASS__ );
  26. }
  27.  
  28. public function main_run()
  29. {
  30. }
  31. }
  32.  
  33. $system = innysystem::get_instance();
  34. $system->main_run();
  35.  
  36. ?>

moze ktos ma lepszy pomysl na dziedziczenie singletona (IMG:http://forum.php.pl/style_emoticons/default/dry.gif)
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: 27.09.2025 - 11:06