MathExpression $a, MathExpression $b)
{
$this->_a = $a;
$this->_b = $b;
}
public function evaluate(array $values)
{
return $this->_a->evaluate($values) * $this->_b->evaluate($values);
}
}
// 10(a + 3)
$expression = new Product(new Literal(10), new Sum(new Variable(''a''), new Literal(3)));
echo $expression->evaluate(array(''a'' => 4)), "\n";
// adding new rules to the grammar is easy:
// e.g. Power, Subtraction...
// thanks to the Composite, manipulation is even simpler:
// we could add substitute($letter, MathExpression $expr)
// to the interface...