Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> wzorzec Rejestr
smiady
post 11.07.2014, 12:59:44
Post #1





Grupa: Zarejestrowani
Postów: 137
Pomógł: 2
Dołączył: 2.07.2007
Skąd: Ostrzeszów

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


Poznałem właśnie wzorzec rejestr i mam taki problem:

register.php
  1. class RegisterException extends Exception {}
  2.  
  3. class Register {
  4. private $store= array();
  5.  
  6. public function set($object, $name= NULL) {
  7. $name= (!is_null($name)) ? $name : get_class($object);
  8. $name= strtolower($name);
  9.  
  10. if($this->contains($name))
  11. return $this->store[$name];
  12.  
  13. $this->store[$name]= $object;
  14.  
  15. return true;
  16. }
  17.  
  18. public function get($name) {
  19. if(!$this->contains($name))
  20. throw new RegisterException('Podany obiekt nie istnieje !');
  21.  
  22. return $this->store[$name];
  23. }
  24.  
  25. public function remove($name) {
  26. if(!$this->contains($name))
  27. throw new RegisterException('Podany obiekt nie istnieje !');
  28.  
  29. unset($this->store[$name]);
  30.  
  31. return true;
  32. }
  33.  
  34. public function contains($name) {
  35. if(isset($this->store[$name]))
  36. return true;
  37.  
  38. return false;
  39. }
  40. }
  41.  
  42. class Klasa {
  43. private $liczba1;
  44.  
  45. public function __construct($integer) {
  46. $this->liczba1= $integer;
  47. }
  48.  
  49. public function setLiczba($integer) {
  50. $this->liczba1= $integer;
  51. }
  52.  
  53. public function getLiczba() {
  54. return $this->liczba1;
  55. }
  56. }


register_start.php
  1.  
  2. require 'register.php';
  3.  
  4. $klasa1= new Klasa(15);
  5.  
  6. $klasa2= clone $klasa1;
  7. $klasa2->setLiczba(30);
  8.  
  9. $klasa3= clone $klasa1;
  10. $klasa3->setLiczba(50);
  11.  
  12. $register= new Register();
  13. $register->set($klasa1, 'klasa1');
  14. $register->set($klasa2, 'klasa2');
  15. $register->set($klasa3, 'klasa3');
  16.  
  17. $_SESSION['register']= $register;
  18.  
  19. echo '<a href="register2.php" title="przejdź do odczytu rejestru">odczyt rejestru</a>';


register2.php
  1.  
  2. require 'register.php';
  3.  
  4. $register= isset($_SESSION['register']) ? $_SESSION['register'] : null;
  5.  
  6. try {
  7. $klasa1= $register->get('klasa1');
  8. $klasa2= $register->get('klasa2');
  9. $klasa3= $register->get('klasa3');
  10. } catch(RegisterException $e) {
  11. echo $e->getMessage();
  12. }
  13.  
  14. echo $klasa1->getLiczba() . '<br/>';
  15. echo $klasa2->getLiczba() . '<br/>';
  16. echo $klasa3->getLiczba() . '<br/>';


Najpierw startuje od register_start.php, potem klikam na linka i mam taki błąd:
Fatal error: main() [<a href='function.main'>function.main</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition &quot;Register&quot; of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in C:\xampp\xampp\htdocs\php\register2.php on line 9
Go to the top of the page
+Quote Post
nospor
post 11.07.2014, 13:04:56
Post #2





Grupa: Moderatorzy
Postów: 36 446
Pomógł: 6292
Dołączył: 27.12.2004




1) Czemu wrzucasz rejestr do sesji? Jest to zbędne raczej
2) Rejestr powinien miec metody statyczne Set, Get, itp dzieki czemu bylby widoczny w calej aplikacji
Register::Set(....)
Register::Get(....)
itd


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
smiady
post 11.07.2014, 13:10:11
Post #3





Grupa: Zarejestrowani
Postów: 137
Pomógł: 2
Dołączył: 2.07.2007
Skąd: Ostrzeszów

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


Ale jak dam Register::set w pliku register_start.php, to Register::get w pliku register2.php nie zwróci mi obiektu - myślę, że rozwiązaniem tutaj jest sesja

Ten post edytował smiady 11.07.2014, 13:10:50
Go to the top of the page
+Quote Post
nospor
post 11.07.2014, 13:14:27
Post #4





Grupa: Moderatorzy
Postów: 36 446
Pomógł: 6292
Dołączył: 27.12.2004




No to cos pomieszales. register_start ma byc odpalany z kazdym żądaniem. Jesli tak bedzie, to wszystko co dodasz do rejestru bedzie widoczne w ramach tego żądania


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
smiady
post 11.07.2014, 13:22:20
Post #5





Grupa: Zarejestrowani
Postów: 137
Pomógł: 2
Dołączył: 2.07.2007
Skąd: Ostrzeszów

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


Chodzi o to, że gdy tworzę nowe pliki PHP i chcę w wyniku jakiegoś działania dodać sobie do magazynu, czyli tego rejestru jakiś obiekt, to muszę go wywołać przez require, albo include - problem w tym, że wtedy otrzymam pusty rejestr bez obiektów - każdy nowy plik PHP i odwołanie się do rejestru to nowy pusty rejestr ...
Go to the top of the page
+Quote Post
nospor
post 11.07.2014, 13:25:51
Post #6





Grupa: Moderatorzy
Postów: 36 446
Pomógł: 6292
Dołączył: 27.12.2004




Jeszcze raz:
rejestr ma funkcjonowac w ramach jednego żądania. Plik rejestru ma byc zawsze dołączony
Do rejestru wrzucasz na poczatku rozne rzeczy: np. obiekt bazy, obiekt wiadomosci, obiekt logow i inne. W innym plikach/klasach odbierasz sobie z rejestru co w danej chwili ci potrzeba.
Jesli chcesz cos wrzucic innego, to tez mozesz, ale musisz to odebrac w ramach tego samego żądania. Rejestr sluzy glownie do trzymania obiektow w ramach tego samego żądania. Nie sluzy do przenoszenia obiektow w sesji miedzy żądaniami. Jak chcesz tak robic, to wszystkie uzyte klasy musisz wpierw zaladowac, co jest bez sensu


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
smiady
post 11.07.2014, 13:55:20
Post #7





Grupa: Zarejestrowani
Postów: 137
Pomógł: 2
Dołączył: 2.07.2007
Skąd: Ostrzeszów

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


A czy teraz takie coś ma większy sens:

register.php
  1. class RegisterException extends Exception {}
  2.  
  3. class Register {
  4. static private $store= array();
  5.  
  6. static public function set($object, $name= NULL) {
  7. $name= (!is_null($name)) ? $name : get_class($object);
  8. $name= strtolower($name);
  9.  
  10. if(self::contains($name))
  11. return self::$store[$name];
  12.  
  13. self::$store[$name]= $object;
  14.  
  15. return true;
  16. }
  17.  
  18. static public function get($name) {
  19. if(!self::contains($name))
  20. throw new RegisterException('Podany obiekt nie istnieje !');
  21.  
  22. return self::$store[$name];
  23. }
  24.  
  25. static public function remove($name) {
  26. if(!self::contains($name))
  27. throw new RegisterException('Podany obiekt nie istnieje !');
  28.  
  29. unset(self::$store[$name]);
  30.  
  31. return true;
  32. }
  33.  
  34. static public function contains($name) {
  35. if(isset(self::$store[$name]))
  36. return true;
  37.  
  38. return false;
  39. }
  40. }
  41.  
  42. class Klasa {
  43. private $liczba1;
  44.  
  45. public function __construct($integer) {
  46. $this->liczba1= $integer;
  47. }
  48.  
  49. public function setLiczba($integer) {
  50. $this->liczba1= $integer;
  51. }
  52.  
  53. public function getLiczba() {
  54. return $this->liczba1;
  55. }
  56. }
  57.  
  58. $klasa1= new Klasa(15);
  59.  
  60. $klasa2= clone $klasa1;
  61. $klasa2->setLiczba(30);
  62.  
  63. $klasa3= clone $klasa1;
  64. $klasa3->setLiczba(50);
  65.  
  66. Register::set($klasa1, 'klasa1');
  67. Register::set($klasa2, 'klasa2');
  68. Register::set($klasa3, 'klasa3');


register_start.php
  1. require 'register.php';
  2.  
  3. try {
  4. $klasa1= Register::get('klasa1');
  5. $klasa2= Register::get('klasa2');
  6. $klasa3= Register::get('klasa3');
  7. } catch(RegisterException $e) {
  8. echo $e->getMessage();
  9. }
  10.  
  11. echo $klasa1->getLiczba() . '<br/>';
  12. echo $klasa2->getLiczba() . '<br/>';
  13. echo $klasa3->getLiczba() . '<br/>';
Go to the top of the page
+Quote Post
nospor
post 11.07.2014, 14:18:16
Post #8





Grupa: Moderatorzy
Postów: 36 446
Pomógł: 6292
Dołączył: 27.12.2004




No nie do konca. w register.php ma byc tylko Register i nic wiecej
Zas w register_start.php ma byc wkladanie obiektow do rejestru a nie odbieranie. Odbierac bedziesz tam, gdzie bedziesz ich potrzebowal


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
smiady
post 22.07.2014, 21:25:12
Post #9





Grupa: Zarejestrowani
Postów: 137
Pomógł: 2
Dołączył: 2.07.2007
Skąd: Ostrzeszów

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


Zastanawiem się nad sensem Rejestru. Skoro odbieram z jednego pliku obiekty metodą get to jaką to daje przewagę w przypadku po prostu tworzenia nowych obiektów ?

Ten post edytował smiady 22.07.2014, 21:25:57
Go to the top of the page
+Quote Post
irmidjusz
post 22.07.2014, 22:00:22
Post #10





Grupa: Zarejestrowani
Postów: 279
Pomógł: 60
Dołączył: 25.02.2012

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


Bo tworzysz je i konfigurujesz raz, a odbierasz wiele razy w różnych miejscach kodu, tam, gdzie ich potrzebujesz.


--------------------
there is much to be learned
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: 19.04.2024 - 13:00