3v4l.org

run code in 200+ PHP versions simultaneously
<?php interface MushroomCalculatorInterface { public function calculateDosage($dose, $wetOrFresh, $MAOi); } class MushroomCalculatorFactory { private $strains = []; private $calculator; public function __construct($strains, $calculator) { $this->strains = $strains; $this->calculator = $calculator; } public function createMushroomCalculator($name): MushroomCalculatorInterface { $potency = $this->strains[$name]; return new $this->calculator($name, $potency); } } class MushroomCalculator implements MushroomCalculatorInterface { private $name; private $potency; public function __construct($name, $potency) { $this->name = $name; $this->potency = $potency; } public function calculateDosage($dose, $wetOrFresh, $MAOi) { $dosages = [ 'microdose' => 0.25, 'regular' => 2.5, 'heroic' => 5, ]; # Pick a dose $dosage = $dosages[$dose]; if ($wetOrFresh) { $dosage *= 9.3; } if ($MAOi) { $dosage /= 2; } return round(($dosage / $this->factor($this->potency)), 2); } private function factor($potency) { $cubensis = 0.63; return $potency / $cubensis; } } $strains = [ 'azurenscens' => 1.78, 'baeocystis' => .85, 'bohemica' => 1.34, 'cubensis' => .63, 'cyanescens' => .85, 'cyanofibrillosa' => .21, 'hoogshagenii' => .60, 'liniformans' => .16, 'penis envy' => 1.35, 'semilanceata' => .98, 'stuntzii' => .36, 'tampanensis' => .68, 'weilii' => .61, ]; $factory= new MushroomCalculatorFactory($strains, MushroomCalculator::class); # foreach (mushroom) { $calculator = $factory->createMushroomCalculator('azurenscens'); echo $calculator->calculateDosage('heroic', true, true); # }
Output for 7.1.25 - 7.3.5
8.23