Design Pattern: Interpreter Pattern in PHP

Interpreter pattern represents the grammar or syntax rules of an expression. Parsing the expression and evaluation is implemented in the pattern implementation.

The implementation can be for a specific syntax of a specific type of expression. A specific set of rules is defined for parsing and evaluation/operation.

In this article, we are discussing the implementation of the Interpreter pattern in PHP. Check the implementation details and examples below.

Implementation

Here are the steps for the implementation in PHP-

  • Define an interface for the operation classes. Define method(s) for the main operation.
  • Create class for terminal operation. Implement the operation interface for the class.
  • Create specific classes for each operation. These classes should also implement the operation interface.

Examples

Check the following examples of the Interpreter pattern implementation in PHP.

Example #1: Logical Operation

Here we are demonstrating the logical operations – AND, OR, XOR. We want to evaluate if certain provided string contains a substring, and then perform the logical operations on that.

Operation Interface

  • Create file “Operation.php”.
  • Define interface “Operation”.
  • Declare method “execute”. This method returns boolean as a result of the evaluation of the logical operation.
<?php
// Operation.php

namespace BigBoxCodeDesignPatternInterpreterLogicalOp;

interface Operation {
    function execute(string $opContext): bool;
}

Terminal Operation Class

  • Create file “TerminalOperation.php”.
  • Define class “TerminalOperation”.
  • Implement the interface “Operatoin” for the class.
  • In the class constructor accept a string param as the main data.
  • In the “execute” method check if the “$data” property contains the provided string as substring or not.
<?php
// TerminalOperation.php

namespace BigBoxCodeDesignPatternInterpreterLogicalOp;


class TerminalOperation implements Operation {

    public function __construct(private string $data) {
    }

    public function execute(string $opContext): bool {
        return str_contains($opContext, $this->data);
    }
}

AND Operation Class

  • Create file “AndOperation.php”.
  • Define class “AndOperation”.
  • Implement interface “Operation” for the class.
  • In the constructor accept 2 params of type “Operation”, and set those to class property.
  • In the execution method perform “AND” operation on the evaluated results.
<?php
// AndOperation.php

namespace BigBoxCodeDesignPatternInterpreterLogicalOp;

class AndOperation implements Operation {
    public function __construct(private Operation $op1, private Operation $op2) {
    }

    public function execute(string $opContext): bool {
        return $this->op1->execute($opContext) && $this->op2->execute($opContext);
    }
}

OR Operation Class

  • Create file “OrOperation.php”.
  • Define class “OrOperation”.
  • Implement interface “Operation” for the class.
  • In the constructor accept 2 params of type “Operation”, and set those to class property.
  • In the execution method perform “OR” operation on the evaluated results.
<?php
// OrOperation.php

namespace BigBoxCodeDesignPatternInterpreterLogicalOp;

class OrOperation implements Operation {
    public function __construct(private Operation $op1, private Operation $op2) {
    }

    public function execute(string $opContext): bool {
        return $this->op1->execute($opContext) || $this->op2->execute($opContext);
    }
}

XOR Operation Class

  • Create file “XorOperation.php”.
  • Define class “XorOperation”.
  • Implement interface “Operation” for the class.
  • In the constructor accept 2 params of type “Operation”, and set those to class property.
  • In the execution method perform “XOR” operation on the evaluated results.
<?php
// XorOperation.php

namespace BigBoxCodeDesignPatternInterpreterLogicalOp;

class XorOperation implements Operation {
    public function __construct(private Operation $op1, private Operation $op2) {
    }

    public function execute(string $opContext): bool {
        return $this->op1->execute($opContext) xor $this->op2->execute($opContext);
    }
}

Demo

In the client we have defined 2 terminal operations “$op1” and “$op2”. For the logical operation “AndOperation”, “OrOperation” and “XorOperation” we have created some objects.

Then we can call the “execute” methods on the operation objects.

<?php
// demo.php

require __DIR__ . '/../../vendor/autoload.php';

use BigBoxCodeDesignPatternInterpreterLogicalOpAndOperation;
use BigBoxCodeDesignPatternInterpreterLogicalOpOrOperation;
use BigBoxCodeDesignPatternInterpreterLogicalOpTerminalOperation;
use BigBoxCodeDesignPatternInterpreterLogicalOpXorOperation;


$op1 = new TerminalOperation("Big");
$op2 = new TerminalOperation("Box");

$andChecker = new AndOperation($op1, $op2);
$orChecker = new OrOperation($op1, $op2);
$xorChecker = new XorOperation($op1, $op2);

$checkStr1 = "Big Box Code";
$checkStr2 = "Only Big Code";
$checkStr3 = "Only Box Code";
$checkStr4 = "No Code";

$andResult1 = $andChecker->execute($checkStr1);
$andResult2 = $andChecker->execute($checkStr2);
$andResult3 = $andChecker->execute($checkStr3);
$andResult4 = $andChecker->execute($checkStr4);

echo "Data: " . $checkStr1 . "; AND Result: " . var_export($andResult1, true) . "n";
echo "Data: " . $checkStr2 . "; AND Result: " . var_export($andResult2, true) . "n";
echo "Data: " . $checkStr3 . "; AND Result: " . var_export($andResult3, true) . "n";
echo "Data: " . $checkStr4 . "; AND Result: " . var_export($andResult4, true) . "n";

$orResult1 = $orChecker->execute($checkStr1);
$orResult2 = $orChecker->execute($checkStr2);
$orResult3 = $orChecker->execute($checkStr3);
$orResult4 = $orChecker->execute($checkStr4);

echo "Data: " . $checkStr1 . "; OR Result: " . var_export($orResult1, true) . "n";
echo "Data: " . $checkStr2 . "; OR Result: " . var_export($orResult2, true) . "n";
echo "Data: " . $checkStr3 . "; OR Result: " . var_export($orResult3, true) . "n";
echo "Data: " . $checkStr4 . "; OR Result: " . var_export($orResult4, true) . "n";

$xorResult1 = $xorChecker->execute($checkStr1);
$xorResult2 = $xorChecker->execute($checkStr2);
$xorResult3 = $xorChecker->execute($checkStr3);
$xorResult4 = $xorChecker->execute($checkStr4);

echo "Data: " . $checkStr1 . "; XOR Result: " . var_export($xorResult1, true) . "n";
echo "Data: " . $checkStr2 . "; XOR Result: " . var_export($xorResult2, true) . "n";
echo "Data: " . $checkStr3 . "; XOR Result: " . var_export($xorResult3, true) . "n";
echo "Data: " . $checkStr4 . "; XOR Result: " . var_export($xorResult4, true) . "n";

Output

The output will be as below-

Data: Big Box Code; AND Result: true
Data: Only Big Code; AND Result: false
Data: Only Box Code; AND Result: false
Data: No Code; AND Result: false


Data: Big Box Code; OR Result: true
Data: Only Big Code; OR Result: true
Data: Only Box Code; OR Result: true
Data: No Code; OR Result: false


Data: Big Box Code; XOR Result: false
Data: Only Big Code; XOR Result: true
Data: Only Box Code; XOR Result: true
Data: No Code; XOR Result: false

Source Code

Use the following link to get the source code:

Other Code Implementations

Use the following links to check Interpreter pattern implementation in other programming languages.

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.