Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>IMHO, you should use Polymorphism.<br> <a href="http://www.youtube.com/watch?v=4F72VULWFvc" rel="noreferrer">This Video</a> may help you understanding this principle</p> <p>Here's my way of thinking.</p> <p>First, define an interface for any operations you'd need</p> <pre><code>interface OperationInterface { public function evaluate(array $operands = array()); } </code></pre> <p>Then, create the calculator holder</p> <pre><code>class Calculator { protected $operands = array(); public function setOperands(array $operands = array()) { $this-&gt;operands = $operands; } public function addOperand($operand) { $this-&gt;operands[] = $operand; } /** * You need any operation that implement the given interface */ public function setOperation(OperationInterface $operation) { $this-&gt;operation = $operation; } public function process() { return $this-&gt;operation-&gt;evaluate($this-&gt;operands); } } </code></pre> <p>Then you can define an operation, for example, addition</p> <pre><code>class Addition implements OperationInterface { public function evaluate(array $operands = array()) { return array_sum($operands); } } </code></pre> <p>And you would use it like :</p> <pre><code>$calculator = new Calculator; $calculator-&gt;setOperands(array(4,2)); $calculator-&gt;setOperation(new Addition); echo $calculator-&gt;process(); // 6 </code></pre> <p>With that point, if you want to add any new behaviour, or modify an existing one, just create or edit a class.</p> <p>For example, say you want a Modulus operation</p> <pre><code>class Modulus implements OperationInterface { public function evaluate(array $operands = array()) { $equals = array_shift($operands); foreach ($operands as $value) { $equals = $equals % $value; } return $equals; } } </code></pre> <p>Then, </p> <pre><code>$calculator = new Calculator; $calculator-&gt;setOperands(array(4,2)); $calculator-&gt;setOperation(new Addition); // 4 + 2 echo $calculator-&gt;process(); // 6 $calculator-&gt;setOperation(new Modulus); // 4 % 2 echo $calculator-&gt;process(); // 0 $calculator-&gt;setOperands(array(55, 10)); // 55 % 10 echo $calculator-&gt;process(); // 5 </code></pre> <hr> <h3>This solution allows your code to be a third-party library</h3> <p>If you plan to reuse this code or give it away as a library, the user wouldn't by any case <em>modify your source code</em>.<br> But what if he wants a <code>Substraction</code> or a <code>BackwardSubstraction</code> method which are not defined ? </p> <p>He just has to create his very own <code>Substraction</code> class in his project, which implements <code>OperationInterface</code> in order to work with your library.</p> <h3>It's easier to read</h3> <p>When looking in the project architecture it's easier to see a folder like this</p> <pre><code>- app/ - lib/ - Calculator/ - Operation/ - Addition.php - Modulus.php - Substraction.php - OperationInterface.php - Calculator.php </code></pre> <p>And immediatly know which file contains the desired behaviour.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload