Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [ OOP ] Singleton z wikipedii nie działa
Babcia@Stefa
post
Post #1





Grupa: Zarejestrowani
Postów: 654
Pomógł: 17
Dołączył: 19.03.2006
Skąd: z kosmosu ;)

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


Witam, użyłem klasy Singleton z wikipedii, ale niestety nie działa.

  1. <?php
  2.  
  3. class Singleton
  4. {
  5.   private static $instance;
  6.   private function __construct() {} // Blokujemy domyślny konstruktor publiczny
  7.   private function __clone(){} //Uniemozliwia utworzenie kopii obiektu
  8.  
  9.   function getInstance()
  10.   {
  11.     return ($i = self::$instance) ? $i : $i = new self;
  12.   }
  13. }
  14. ?>


Czy ona nie powinna wyglądać tak?

  1. <?php
  2.  
  3. class Singleton
  4. {
  5.   private static $instance;
  6.   private function __construct() {} // Blokujemy domyślny konstruktor publiczny
  7.   private function __clone(){} //Uniemozliwia utworzenie kopii obiektu
  8.  
  9.    function getInstance()
  10.    {
  11.    return ($i = self::$instance) ? $i : $i = self::$instance = new self;
  12.    }
  13. }
  14. ?>


Przy użyciu:
  1. <?php
  2. class myClass extends Singleton
  3. {
  4. (...)
  5. }
  6.  
  7. $Object = myClass::getInstance();
  8. ?>


Albo źle zrozumiałem pojęcie "Singletonów" albo jest błąd na wikipedii (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Jeśli się mylę, proszę o wskazanie mi błędu (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

Pozdrawiam, WebNuLL

Ten post edytował Babcia@Stefa 16.01.2009, 17:29:46
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 - 13:06