Visitor Design Pattern in PHP

1. Definition

The Visitor Design Pattern allows you to add further operations to objects without having to modify them. It involves a visitor class that changes the executing algorithm of an element class.

2. Problem Statement

Imagine you have a structure of many objects that form a composite. You realize that you need to perform operations on these objects that don't necessarily pertain to the object itself. If you add these operations directly to the objects, it might bloat them with unrelated functionality.

3. Solution

The Visitor pattern suggests that you place the new behavior into a separate class called a visitor, instead of trying to integrate it into existing classes. The original object that had to perform the behavior is now passed to one of the visitor's methods as an argument, providing the method access to all necessary information contained within the object.

4. Real-World Use Cases

1. Syntax tree representation in compilers: Different visitors can process a syntax tree in various ways, like optimization, code generation, etc.

2. Rendering graphical objects: Different visitors could render objects in different formats.

5. Implementation Steps

1. Declare a visitor interface with a visit method for every concrete element class.

2. Declare the element interface, which includes an accept method that takes a visitor as an argument.

3. Implement the accept method in concrete elements. This method simply redirects the call to a visit method on the incoming visitor object.

4. Create concrete visitor classes that implement each operation.

6. Implementation in PHP

// Element interface
interface Element {
    public function accept(Visitor $visitor);
// Concrete elements
class ElementA implements Element {
    public function accept(Visitor $visitor) {
    public function operationA() {
        return "ElementA operation";
class ElementB implements Element {
    public function accept(Visitor $visitor) {
    public function operationB() {
        return "ElementB operation";
// Visitor interface
interface Visitor {
    public function visitElementA(ElementA $element);
    public function visitElementB(ElementB $element);
// Concrete visitor
class ConcreteVisitor implements Visitor {
    public function visitElementA(ElementA $element) {
        echo "Visitor on " . $element->operationA() . "\n";
    public function visitElementB(ElementB $element) {
        echo "Visitor on " . $element->operationB() . "\n";
// Client code
$elements = [new ElementA(), new ElementB()];
$visitor = new ConcreteVisitor();
foreach ($elements as $element) {


Visitor on ElementA operation
Visitor on ElementB operation


1. Element is the interface that all elements must implement. It defines the accept method that takes a visitor.

2. ElementA and ElementB are concrete elements. They implement the accept method and have their own unique methods (operationA and operationB).

3. Visitor is the interface for visitors. It has a visit method for each concrete element.

4. ConcreteVisitor is a visitor that implements the actions to be executed on each element.

5. In the client code, we apply the ConcreteVisitor to a list of elements, demonstrating the flexibility of the Visitor pattern.

7. When to use?

Use the Visitor pattern when:

1. You need to perform operations on objects without changing their classes.

2. The set of objects rarely changes but you often want to define new operations.

3. The classes defining the objects are not suited to these operations and don't have the necessary information on their own.