Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [ OOP ] Singleton z wikipedii nie działa
Babcia@Stefa
post 16.01.2009, 17:28:37
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 tongue.gif
Jeśli się mylę, proszę o wskazanie mi błędu smile.gif

Pozdrawiam, WebNuLL

Ten post edytował Babcia@Stefa 16.01.2009, 17:29:46


--------------------
Środowisko testowe (desktop) - Gedit, lighttpd, sftp, rsync, xfce4-terminal, chromium, firefox4 | System: Gentoo ~x86
O'Neill - serwer WWW @ lighttpd, links, nano, rsyncd, sftpd | System: Debian
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
starach
post 16.01.2009, 21:32:55
Post #2





Grupa: Zarejestrowani
Postów: 999
Pomógł: 30
Dołączył: 14.01.2007
Skąd: wiesz ?

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


  1. <?php
  2. class Singleton_Extended extends Singleton
  3. {
  4.    public function setVar($value)
  5.    {
  6.        parent::setVar($value + 1);
  7.    }
  8. }
  9.  
  10. class Singleton
  11. {
  12.    private $var;
  13.    private function __construct() {}
  14.    private function __clone() {}
  15.    function getInstance()
  16.    {
  17.        static $instance;
  18.        return empty($instance) ? $instance = new self : $instance;
  19.    }
  20.    public function setVar($value)
  21.    {
  22.        $this->var = $value;
  23.    }
  24.    public function getVar()
  25.    {
  26.        return $this->var;
  27.    }
  28. }
  29.  
  30. Singleton::getInstance()->setVar(1);
  31. echo Singleton::getInstance()->getVar();
  32. // Zwróci: "1"
  33.  
  34. Singleton_Extended::getInstance()->setVar(1);
  35. echo Singleton_Extended::getInstance()->getVar();
  36. // Zwróci: "1" bo tak czy srak trzeba nadpisac implementacje singletona
  37. ?>


Kto dodał taki beznadziejny kod na wiki. :|
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: 9.06.2024 - 10:13