Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> problem z działaniem metody
damianooo
post
Post #1





Grupa: Zarejestrowani
Postów: 496
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


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

Ten post edytował damianooo 22.07.2013, 15:31:09
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
nospor
post
Post #2





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




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


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

"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
damianooo
post
Post #3





Grupa: Zarejestrowani
Postów: 496
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


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
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 Aktualny czas: 19.08.2025 - 19:54