Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Klasa zwraca samą siebie
Forum PHP.pl > Forum > PHP
tadeurz
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);
nospor
nie: this
a: $this
...

Jak zwykle w manualu takie rzeczy są bardzo dobrze wyjaśnione.
tadeurz
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.
domo
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. ?>
tadeurz
Wszytkie Twoje uwagi są słuszne.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.