<?php
interface Visitor
{
public function visitCar(Car $car);
public function visitGolf(Golf $golf);
public function visitPorsche(Porsche $porsche);
}
interface Vehicle
{
public function accept(Visitor $v);
}
class Car implements Vehicle
{
public function accept(Visitor $v)
{
$v->visitCar($this);
}
}
class Golf extends Car
{
public function accept(Visitor $v)
{
$v->visitGolf($this);
}
}
class Porsche extends Car
{
public function accept(Visitor $v)
{
$v->visitPorsche($this);
}
}
class DriveVisitor implements Visitor
{
public function visitCar(Car $car)
{
print 'Auto fährt' . PHP_EOL;
}
public function visitGolf(Golf $golf)
{
print 'Golf fährt schneller' . PHP_EOL;
}
public function visitPorsche(Porsche $porsche)
{
print 'Auto fährt am schnellsten' . PHP_EOL;
}
}
$c = new Car();
$g = new Golf();
$p = new Porsche();
$dv = new DriveVisitor();
$c->accept($dv);
$g->accept($dv);
$p->accept($dv);