3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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 'Porsche 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);
based on mUXhq
Output for 5.3.22 - 7.2.0
Auto fährt Golf fährt schneller Porsche fährt am schnellsten