3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface GoAlgorithm { public function go(); } class GoByDrivingAlgorithm implements GoAlgorithm { public function go() { echo "Now I'm driving." .PHP_EOL; } } class GoByFlying implements GoAlgorithm { public function go() { echo "Now I'm flying." . PHP_EOL; } } class GoByFlyingFast implements GoAlgorithm { public function go() { echo "Now I'm flying fast" . PHP_EOL; } } class Vehicle { private $goAlgorithm; public function __construct(GoAlgorithm $goType = null) { $this->goAlgorithm = $goType; } public function setModeOfTransportation(GoAlgorithm $goType) { $this->goAlgorithm = $goType; } public function drive() { $this->goAlgorithm->go(); } } // setter injection $streetRacer = new Vehicle(); $streetRacer->setModeOfTransportation(new GoByDrivingAlgorithm()); $formulaone = new Vehicle(); $formulaone->setModeOfTransportation(new GoByDrivingAlgorithm()); $helicopter = new Vehicle(); $helicopter->setModeOfTransportation(new GoByFlying()); $jets = new Vehicle(); $jets->setModeOfTransportation(new GoByFlyingFast()); $streetRacer->drive(); $formulaone->drive(); $helicopter->drive(); $jets->drive(); // constructor injection $streetRacerByCtor = new Vehicle(new GoByDrivingAlgorithm()); $formulaoneByCtor = new Vehicle(new GoByDrivingAlgorithm()); $helicopterByCtor = new Vehicle(new GoByFlying()); $jetsByCtor = new Vehicle(new GoByFlyingFast()); $streetRacerByCtor->drive(); $formulaoneByCtor->drive(); $helicopterByCtor->drive(); $jetsByCtor->drive();
Output for git.master, git.master_jit, rfc.property-hooks
Now I'm driving. Now I'm driving. Now I'm flying. Now I'm flying fast Now I'm driving. Now I'm driving. Now I'm flying. Now I'm flying fast

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
51.35 ms | 401 KiB | 8 Q