Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [ 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
dr_bonzo
post 16.01.2009, 17:33:32
Post #2





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


Jeszcze brakuje $i+++++++b z C.

zapisz tego ifa w sposob czytelniejszy ibedziesz mial dobra wersje:

jesli self::$instance jest null { to utworz nowy obiekt }

a potem zwroc go


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
Babcia@Stefa
post 16.01.2009, 17:35:46
Post #3





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

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


Ten if to oryginał skopiowany z wikipedii tongue.gif (nie mój)
A tak po za tym to słyszałem że skrócone ify są szybsze od tych normalnych, ale nie jestem pewien.

Ale ten nowy obiekt wtedy wrzucam do self::$instance aby go później zwrócić raczej tongue.gif

@edit
Cytat(orglee)
Kto dodał taki beznadziejny kod na wiki. :|


Na pewno nie ja winksmiley.jpg

Pozdrawiam, WebNuLL

Ten post edytował Babcia@Stefa 20.01.2009, 23:09:42


--------------------
Ś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
dr_bonzo
post 16.01.2009, 17:39:53
Post #4





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


Wiem ze kod z wikipedii - zajrzalem tam przed odpowiedzeniem.

skrocone ify szybsze, yhym, przez 10 min jeszcze nie zalapalem jak ten kod mialby sie wykonac, co oznacza ze ten zapis jest mega nieczytelny.

(oczywiscie $x = ($newX ? $newX : "default" ) jest dla mnie jak najbardziej dopuszczalny, w innych przypadkach nie uzywam skroconego ifa)

edit: tak, w ifie wrzucasz obiekt do self::$instance

Ten post edytował dr_bonzo 16.01.2009, 17:40:51


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
starach
post 16.01.2009, 21:32:55
Post #5





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

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: 20.04.2024 - 14:41