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 ? ...
class Calculator
{
private $lefthandoperator;
private $righthandoperator;
private $operation;
public function __construct($operation, $lefthandoperator, $righthandoperator)
{
$this->operation = $operation;
$this->lefthandoperator = $lefthandoperator;
$this->righthandoperator = $righthandoperator;
}
private function add()
{
return $this->lefthandoperator + $this->righthandoperator;
}
private function subtract()
{
return $this->lefthandoperator - $this->righthandoperator;
}
private function multiply()
{
return $this->lefthandoperator * $this->righthandoperator;
}
private function divide()
{
return $this->lefthandoperator / $this->righthandoperator;
}
private function logicaland()
{
return $this->lefthandoperator && $this->righthandoperator;
}
private function logicalor()
{
return $this->lefthandoperator || $this->righthandoperator;
}
public function calculate()
{
switch ($this->operation) {
case 'add':
$this->add();
break;
case 'subtract':
$this->subtract();
break;
case 'multiply':
$this->multiply();
break;
case 'divide':
$this->divide();
break;
case 'logicaland':
$this->logicaland();
break;
case 'logicalor':
$this->logicalor();
break;
}
}
}
$calculator = new Calculator('add', 4, 2);
$calculator->calculate();
dzięki
Ten post edytował damianooo 22.07.2013, 15:31:09