Design Patterns PHP - Interpreter Java

1) What have we done?

In order to make system interpreting expressions we need to little rethink about following expression:

(a+b)*4+2*c

what elements we can find in our expression:

a, b, c - Variable
4,2 - Constant
+ - Add (two arguments, both Vectors)
* - Multiply(two arguments, one Vectors, second Scalar)

And here we apply "Interpreter" first of this pattern tells us to create exactly class like these elements:
- Variable
- Constant
- Add
- Multiply

All will implements "Expression" interface which has a method "evaluate" for interpret given expression

interface Expression
{
    /**
     * @param Vector[] $variables
     * @return mixed
     */
    public function evaluate($variables);
}

class Variable implements Expression
{
    /**
     * @var string
     */
    protected $letter;

    /**
     * @param string $letter
     */
    function __construct($letter)
    {
        $this->letter = $letter;
    }

    /**
     * @param Vector[] $variables
     * @return mixed|Vector
     */
    public function evaluate($variables)
    {
        return $variables[$this->letter];
    }
}

class Constant implements Expression
{
    /**
     * @var int
     */
    protected $integer;

    function __construct($integer)
    {
        $this->integer = $integer;
    }

    public function evaluate($variables)
    {
        return $this->integer;
    }
}


class Add implements Expression
{
    /**
     * @var Expression
     */
    protected $expression1;

    /** @var  Expression */
    protected $expression2;

    /**
     * @param Expression $expression1
     * @param Expression $expression2
     */
    function __construct(Expression $expression1, Expression $expression2)
    {
        $this->expression1 = $expression1;
        $this->expression2 = $expression2;
    }

    public function evaluate($variables)
    {
        return $this->expression1->evaluate($variables)
            ->add($this->expression2->evaluate($variables));
    }
}

class Multiply implements Expression
{
    /**
     * @var Expression
     */
    protected $expression1;

    /**
     * @var Expression
     */
    protected $expression2;

    /**
     * @param Expression $expression1
     * @param Expression $expression2
     */
    function __construct($expression1, $expression2)
    {
        $this->expression1 = $expression1;
        $this->expression2 = $expression2;
    }

    public function evaluate($variables)
    {
        if (!$this->expression1 instanceof Constant) {
            return $this->expression1
                ->evaluate($variables)
                ->multiply($this->expression2->evaluate($variables));
        } else {
            return $this->expression2
                ->evaluate($variables)
                ->multiply($this->expression1->evaluate($variables));
        }

    }

}



2) How is expression built ?
Let's for example: (a+b)*4+2*c

Step by Step:

(1) Adding - a+b
(2) Multiplication - (1) i 4
(3) Multiplication - 2 i c
(4) Adding - (2) i (3)

So we can do this like the following, it is as we create tree:


//(a+b)*4+2*c
$expression = new Add(new Multiply(new Add(new Variable('a'), new Variable('b')), new Constant(4)), new Multiply(new Constant(2), new Variable('c')));

$vector = $expression->evaluate([
    'a' => new Vector(2, 3),
    'b' => new Vector(3, 5),
    'c' => new Vector(1, 2)
]);