3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class CommandFactory { private $commandMappings; public function __construct(string $mappingFilename = 'default.json') { //$mappingsJSON = file_get_content($mappingFilename); $this->commandMappings = json_decode("{ 'command' : { 'SYSTEMS' : { 'OS1' : { 'Version' : 'command string', 'Version2' : 'command string', }, 'OS2' : { 'Version' : 'command string', 'Version' : 'command string' }, 'COMMANDOPTIONS' : ['var1', 'var2', 'var3'], }, 'add-Firewall' : { 'SYSTEMS' : { 'Ubuntu' : { '14' : 'command string', '11' : 'command string', }, 'Redhat' : { '13' : 'command string', '11' : 'command string' }, 'COMMANDOPTIONS' : ['port',], }, }", true); } public function getCommand(string $command, array $options): Command { $commandArray = $this->commandMappings[$command]; return new Command($commandArray, $options); } } class Command { private $options; private $mapping; public function __construct($mappings, $options) { var_dump($mappings); //$this->$options = $options; //$this->mapping = $mappings; // check if options are are valid // potentially map in options here? } public function view(string $os, $version): string { // validate $os and $version $command = $this->mapping['SYSTEMS'][$os][$version]; //$command = str_replace(array_keys($options), $options, $command); return $command; } } // usage $factory = new CommandFactory(); $firewallCommand = $factory->getCommand('add-Firewall', ['port' => 23]); echo $firewallCommand->view('Ubuntu', '11');
Output for 7.0.0 - 7.2.9
NULL Fatal error: Uncaught TypeError: Return value of Command::view() must be of the type string, null returned in /in/O2P3t:69 Stack trace: #0 /in/O2P3t(76): Command->view('Ubuntu', '11') #1 {main} thrown in /in/O2P3t on line 69
Process exited with code 255.
Output for hhvm-3.18.5, hhvm-3.22.0
NULL Fatal error: Uncaught TypeError: Value returned from method Command::view() must be of type string, null given in /in/O2P3t:-1 Stack trace: #0 /in/O2P3t(-1): Command->view() #1 {main}
Process exited with code 255.
Output for hhvm-3.15.4 - 3.18.4, hhvm-3.19.2 - 3.21.3
NULL Fatal error: Uncaught TypeError: Value returned from method Command::view() must be of type string, null given in /in/O2P3t:69 Stack trace: #0 /in/O2P3t(76): Command->view() #1 {main}
Process exited with code 255.
Output for 5.6.30
Fatal error: Default value for parameters with a class type hint can only be NULL in /in/O2P3t on line 8
Process exited with code 255.