Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: problem z działaniem metody
Forum PHP.pl > Forum > Przedszkole
damianooo
Witam,
Mam taką oto klasę. Nie wiem czemu gdy sprawdzam var_dump wywołanie metody calculate() to wyrzuca mi NULL ? Czy dobry mam ten zapis klasy czy należy to zrobić trochę inaczej ? ...

  1. class Calculator
  2. {
  3. private $lefthandoperator;
  4. private $righthandoperator;
  5. private $operation;
  6.  
  7. public function __construct($operation, $lefthandoperator, $righthandoperator)
  8. {
  9. $this->operation = $operation;
  10. $this->lefthandoperator = $lefthandoperator;
  11. $this->righthandoperator = $righthandoperator;
  12. }
  13.  
  14. private function add()
  15. {
  16. return $this->lefthandoperator + $this->righthandoperator;
  17. }
  18.  
  19. private function subtract()
  20. {
  21. return $this->lefthandoperator - $this->righthandoperator;
  22. }
  23.  
  24. private function multiply()
  25. {
  26. return $this->lefthandoperator * $this->righthandoperator;
  27. }
  28.  
  29. private function divide()
  30. {
  31. return $this->lefthandoperator / $this->righthandoperator;
  32. }
  33.  
  34. private function logicaland()
  35. {
  36. return $this->lefthandoperator && $this->righthandoperator;
  37. }
  38.  
  39. private function logicalor()
  40. {
  41. return $this->lefthandoperator || $this->righthandoperator;
  42. }
  43.  
  44. public function calculate()
  45. {
  46. switch ($this->operation) {
  47. case 'add':
  48. $this->add();
  49. break;
  50. case 'subtract':
  51. $this->subtract();
  52. break;
  53. case 'multiply':
  54. $this->multiply();
  55. break;
  56. case 'divide':
  57. $this->divide();
  58. break;
  59. case 'logicaland':
  60. $this->logicaland();
  61. break;
  62. case 'logicalor':
  63. $this->logicalor();
  64. break;
  65. }
  66. }
  67. }
  68.  
  69.  
  70. $calculator = new Calculator('add', 4, 2);
  71. $calculator->calculate();
  72.  
  73. var_dump($calculator->calculate());
  74.  
  75.  








dzięki
nospor
Przeciez metoda calculate nic nie zwraca to czemu sie dziwisz ze dostajesz null? O RETURN nie slyszal? Przenosze na przedszkole - problem z obiektowką nie ma zadnego zwiazku
damianooo
dobra sorry , zrobiłem "return" w metodach prywatnych ale już w głównej metodzie odpalającej poszczególne metody w zależności od tego co jest w switchu nie dałem return sad.gif porażka ,

dzięki
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.