Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Klasa zwraca samą siebie
tadeurz
post 4.03.2013, 01:45:35
Post #1





Grupa: Zarejestrowani
Postów: 70
Pomógł: 1
Dołączył: 25.04.2009

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


Mam problem, nie wiem czy jest to wogóle możliwe.Mam 2 klasy Element i Box, chce żeby wywołanie metody Element.save() zwróciło samą siebie do metody Box.storeElements()
  1. class Box{
  2. public static function storeElement($element){
  3. if( $element->type == 'red' ) self::goToRedElements();
  4. else self::goToDefaultElements();
  5. }
  6. }
  7. class Element{
  8. private type = 'default';
  9. public function save(){
  10. $this->type = 'red';
  11. box::storeElement(this); //tutaj jak zwrócić instancje obiektu Element ?
  12. }
  13. }
  14. /**************************/
  15. $element = new Element();
  16. $element->save();
  17.  
  18. //nie chce tego robić tak:
  19. $element = new Element();
  20. $element->save();
  21. //z metody save() wylatuje wywołanie box::storeElement();
  22. box::storeElement($element);


Ten post edytował tadeurz 4.03.2013, 02:00:53
Go to the top of the page
+Quote Post
nospor
post 4.03.2013, 07:48:21
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




nie: this
a: $this
...

Jak zwykle w manualu takie rzeczy są bardzo dobrze wyjaśnione.


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

"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
tadeurz
post 4.03.2013, 10:34:51
Post #3





Grupa: Zarejestrowani
Postów: 70
Pomógł: 1
Dołączył: 25.04.2009

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


Oczywiście masz racje.
Co ciekawsze teraz tak na świeżo jak przeglądniemy się zwróconej notce i dokładnie przeczytamy:
  1. Notice: Use of undefined constant this - assumed 'this' in /xxx/xxxxx.php on line xx


I zastanowimy dlaczego pisze coś o stałej, cały problem rozwiązany.
Go to the top of the page
+Quote Post
domo
post 4.03.2013, 10:55:33
Post #4





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 25.12.2007

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


po pierwsze źle definiujesz składową 'type' klasy Element
zamiast
  1. private type = 'default';

powinno być
  1. private $type = 'default';



w metodzie save() ponownie źle przekazujesz parametr.
zamiast:
  1. public function save(){
  2. $this->type = 'red';
  3. box::storeElement(this); //tutaj jak zwrócić instancje obiektu Element ?
  4. }


powinno być
  1. public function save(){
  2. $this->type = 'red';
  3. box::storeElement($this); //tutaj jak zwrócić instancje obiektu Element ?
  4. }



Kolejnym błędem jest odwoływanie się do prywatnej składowej 'type' klasy Element w sposób jaki to robimy dla publicznych składowych.
  1. public static function storeElement($element){
  2. if ($element->type == 'red' ) self::goToRedElements(); // type jest prywatna!
  3. else self::goToDefaultElements();
  4. }



Jeśli chcesz mieć dostęp tylko do odczytu wartości 'type' musisz dodać metodę do klasy Element zwracającą tę wartość.

np:
  1. public function getType()
  2. {
  3. return $this->type;
  4. }



Poniżej wklejam poprawnie napisany kod:

  1. <?php
  2.  
  3. class Box
  4. {
  5. public static function storeElement($element){
  6. if ($element->getType() == 'red') {
  7. self::goToRedElements();
  8. } else {
  9. self::goToDefaultElements();
  10. }
  11. }
  12. }
  13. class Element
  14. {
  15. private $type = 'default';
  16.  
  17. public function getType()
  18. {
  19. return $this->type;
  20. }
  21.  
  22. public function save(){
  23. $this->type = 'red';
  24. box::storeElement($this); //tutaj jak zwrócić instancje obiektu Element ?
  25. }
  26. }
  27. /**************************/
  28. $element = new Element();
  29. $element->save();
  30.  
  31. //nie chce tego robić tak:
  32. $element = new Element();
  33. $element->save();
  34. //z metody save() wylatuje wywołanie box::storeElement();
  35. box::storeElement($element);
  36.  
  37. ?>


Ten post edytował domo 4.03.2013, 11:00:04
Go to the top of the page
+Quote Post
tadeurz
post 7.03.2013, 23:06:12
Post #5





Grupa: Zarejestrowani
Postów: 70
Pomógł: 1
Dołączył: 25.04.2009

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


Wszytkie Twoje uwagi są słuszne.
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.07.2025 - 01:56