Visitor is one of the less commonly used GoF patterns, but it solves a specific problem elegantly: adding new operations to an object hierarchy without modifying the hierarchy itself. Double dispatch – calling different methods based on both the visitor type and the element type – is the mechanism. I show a PHP implementation and apply it to a real scenario: exporting product data to CSV and PDF without adding export logic to the product classes.
The problem Visitor solves
<?php
// We have a product hierarchy
abstract class Product { abstract public function getPrice(): float; }
class SimpleProduct extends Product { public function getPrice(): float { return 9.99; } }
class BundleProduct extends Product { public function getPrice(): float { return 29.99; } }
class VirtualProduct extends Product { public function getPrice(): float { return 4.99; } }
// We need to export them to CSV, PDF, XML, JSON...
// WITHOUT adding export methods to every Product class
// Bad approach: add to the class hierarchy
class SimpleProduct extends Product
{
public function exportCsv(): string { ... }
public function exportPdf(): string { ... }
public function exportXml(): string { ... }
// Product class becomes a dumping ground for unrelated concerns
}
Visitor implementation – double dispatch
<?php
declare(strict_types=1);
// Visitor interface - one method per element type
interface ProductVisitorInterface
{
public function visitSimple(SimpleProduct $product): void;
public function visitBundle(BundleProduct $product): void;
public function visitVirtual(VirtualProduct $product): void;
}
// Element interface - accept any visitor
interface ProductInterface
{
public function accept(ProductVisitorInterface $visitor): void;
public function getSku(): string;
public function getName(): string;
public function getPrice(): float;
}
class SimpleProduct implements ProductInterface
{
public function __construct(
private string $sku,
private string $name,
private float $price,
private float $weight
) {}
public function getSku(): string { return $this->sku; }
public function getName(): string { return $this->name; }
public function getPrice(): float { return $this->price; }
public function getWeight(): float { return $this->weight; }
// Double dispatch: the element calls the visitor method specific to its own type
public function accept(ProductVisitorInterface $visitor): void
{
$visitor->visitSimple($this);
}
}
class BundleProduct implements ProductInterface
{
public function __construct(
private string $sku,
private string $name,
private float $price,
private array $childSkus = []
) {}
public function getSku(): string { return $this->sku; }
public function getName(): string { return $this->name; }
public function getPrice(): float { return $this->price; }
public function getChildSkus(): array { return $this->childSkus; }
public function accept(ProductVisitorInterface $visitor): void
{
$visitor->visitBundle($this); // dispatches to bundle-specific method
}
}
class VirtualProduct implements ProductInterface
{
public function __construct(
private string $sku,
private string $name,
private float $price,
private string $downloadUrl
) {}
public function getSku(): string { return $this->sku; }
public function getName(): string { return $this->name; }
public function getPrice(): float { return $this->price; }
public function getDownloadUrl(): string { return $this->downloadUrl; }
public function accept(ProductVisitorInterface $visitor): void
{
$visitor->visitVirtual($this);
}
}
Concrete Visitors – operations without modifying classes
<?php
declare(strict_types=1);
// CSV Export Visitor
class CsvExportVisitor implements ProductVisitorInterface
{
private array $rows = [];
public function visitSimple(SimpleProduct $product): void
{
$this->rows[] = implode(',', [
$product->getSku(),
$product->getName(),
$product->getPrice(),
'simple',
$product->getWeight(),
'', // no download URL
]);
}
public function visitBundle(BundleProduct $product): void
{
$this->rows[] = implode(',', [
$product->getSku(),
$product->getName(),
$product->getPrice(),
'bundle',
'', // no weight
implode('|', $product->getChildSkus()),
]);
}
public function visitVirtual(VirtualProduct $product): void
{
$this->rows[] = implode(',', [
$product->getSku(),
$product->getName(),
$product->getPrice(),
'virtual',
'', // no weight
$product->getDownloadUrl(),
]);
}
public function getCsv(): string
{
$header = "sku,name,price,type,weight,extra";
return $header . "\n" . implode("\n", $this->rows);
}
}
// Tax Calculator Visitor - different tax rules per product type
class TaxCalculatorVisitor implements ProductVisitorInterface
{
private float $totalTax = 0.0;
public function visitSimple(SimpleProduct $product): void
{
$this->totalTax += $product->getPrice() * 0.23; // 23% VAT on physical goods
}
public function visitBundle(BundleProduct $product): void
{
$this->totalTax += $product->getPrice() * 0.23; // 23% VAT
}
public function visitVirtual(VirtualProduct $product): void
{
$this->totalTax += $product->getPrice() * 0.23; // 23% VAT on digital goods (EU)
}
public function getTotalTax(): float { return round($this->totalTax, 2); }
}
// Usage
$products = [
new SimpleProduct('MG-001', 'Widget', 29.99, 0.5),
new BundleProduct('MG-002', 'Bundle', 49.99, ['MG-001', 'MG-003']),
new VirtualProduct('MG-003', 'License', 9.99, 'https://cdn.example.com/dl/abc'),
];
// Export to CSV - without touching Product classes
$csvVisitor = new CsvExportVisitor();
foreach ($products as $product) {
$product->accept($csvVisitor);
}
echo $csvVisitor->getCsv();
// Calculate tax
$taxVisitor = new TaxCalculatorVisitor();
foreach ($products as $product) {
$product->accept($taxVisitor);
}
echo "Total tax: " . $taxVisitor->getTotalTax() . "\n"; // 20.68
When to use Visitor
- You have a stable class hierarchy (Product types) but frequently add new operations (export, validate, index, price rules)
- Operations on different class types require different logic for each type
- You cannot or do not want to modify the element classes
Visitor is overkill when the hierarchy changes frequently (every new product type requires updating all visitors) or when operations are simple enough for a switch/match statement.
Summary
Visitor separates algorithms from the objects they operate on. The double dispatch mechanism – element calls the visitor method matching its own type – is elegant and ensures type-specific behaviour without instance checks. In Magento 2 context, this pattern fits product export pipelines, validation chains, and price calculation rules where the product type determines the logic. Adding a new operation is a new class; no existing code changes.
