![]() |
![]() |
![]()
Post
#1
|
|
Grupa: Zarejestrowani Postów: 43 Pomógł: 0 Dołączył: 30.05.2003 Skąd: Kraków Ostrzeżenie: (0%) ![]() ![]() |
Więc tak. Chciałem zrobić sobie klasę Module, po której dziedziczą wszelkie moduły. Klasa module miałaby mieć metodę getReference() zwracającą metodę do siebie samego. No i ten dziedziczący moduł miałby dziedziczyć tą metodę. I teraz sprawa wygląda tak:
[php:1:4616761559]<?php class Module { private static $myReference; public function &getReference() { if(!isset(self::$myReference)) self::$myReference = new self; return self::$myReference; } } class User extends Module { // Zmienne odpowiadające za różne dane z formularza logowania public $login; public function __construct() { $this->login = "Foo"; } } $normal = new User; $singleton = User::getReference(); ?> <pre> <?php var_dump($normal); var_dump($singleton); ?> </pre>[/php:1:4616761559] Wynik? Cytat object(User)#1 (4) {
["login"]=> string(3) "Foo" } object(Module)#2 (0) { } No i nie wiem za bardzo jak zrobić, żeby $singleton było obiektem user. Czy da się wogóle tak zaprojektować klasę Module? |
|
|
![]() |
![]()
Post
#2
|
|
Grupa: Zarejestrowani Postów: 270 Pomógł: 0 Dołączył: 15.06.2003 Ostrzeżenie: (0%) ![]() ![]() |
a wziąłeś od uwage jak sie powyższy kod zachowa jak będą conajmniej 2 obiekty??. $myReference powinno być tablicą.
[php:1:41a4877a81]<?php class Module { private static $myReference = array(); public function &getReference($class) { if(!isset(self::$myReference[$class])) self::$myReference[$class] = new $class; return self::$myReference[$class]; } } class User { // Zmienne odpowiadające za różne dane z formularza logowania public $login; public function __construct() { $this->login = "Foo"; } } $normal = Module::getReference('user'); $normal->test='ok'; $singleton = Module::getReference('user'); ?> <pre> <?php var_dump($normal); var_dump($singleton); ?> </pre> ?>[/php:1:41a4877a81] Kod object(User)#1 (2) {
["login"]=> string(3) "Foo" ["test"]=> string(2) "ok" } object(User)#1 (2) { ["login"]=> string(3) "Foo" ["test"]=> string(2) "ok" } Jeszcze nie bawiłem sie na poważnie php5 ale powinno działac |
|
|
![]() ![]() |
![]() |
Aktualny czas: 30.09.2025 - 17:30 |