3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface ConfigValueInterface { public function __toString(); } interface ConfigWriterInterface { public function write(ConfigValueInterface $value); } class WriteManager { protected $writers = []; public function addWriter(ConfigWriterInterface $writer, $value_class) { $this->writers[$value_class] = $writer; } public function write(ConfigValueInterface $value) { $class = get_class($value); if (isset($this->writers[$class])) { $this->writers[$class]->write($value); } } } class BaseConfigValue implements ConfigValueInterface { public $storage_location; public function __toString() { return "derp"; } } class NginxConfigValue extends BaseConfigValue {} class BaseConfigWriter implements ConfigWriterInterface { public function write(ConfigValueInterface $value) { if (!$value instanceof BaseConfigValue) { throw new \RuntimeException('Invalid value type'); } echo "writing to {$value->storage_location}"; } } class NginxConfigWriter implements ConfigWriterInterface { public function write(ConfigValueInterface $value) { if (!$value instanceof NginxConfigValue) { throw new \RuntimeException('Invalid value type'); } echo "writing NGINX config to {$value->storage_location}"; } } class ExampleGenerator { public function generate() { $value = new BaseConfigValue(); $value->storage_location = "/some/path/sample.config"; return $value; } } class NginxGenerator { public function generate() { $value = new NginxConfigValue(); $value->storage_location = "/some/path/sample.config"; return $value; } } // Set up manager $manager = new WriteManager(); $manager->addWriter(new BaseConfigWriter, BaseConfigValue::class); $manager->addWriter(new NginxConfigWriter, BaseConfigValue::class); $generator = new ExampleGenerator(); $value = $generator->generate(); $manager->write($value); $generator = new NginxGenerator(); $value = $generator->generate(); $manager->write($value);
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught RuntimeException: Invalid value type in /in/RQCMY:68 Stack trace: #0 /in/RQCMY(29): NginxConfigWriter->write(Object(BaseConfigValue)) #1 /in/RQCMY(111): WriteManager->write(Object(BaseConfigValue)) #2 {main} thrown in /in/RQCMY on line 68
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Uncaught RuntimeException: Invalid value type in /in/RQCMY:68 Stack trace: #0 /in/RQCMY(29): NginxConfigWriter->write(Object(BaseConfigValue)) #1 /in/RQCMY(111): WriteManager->write(Object(BaseConfigValue)) #2 {main} thrown in /in/RQCMY on line 68
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Fatal error: Uncaught exception 'RuntimeException' with message 'Invalid value type' in /in/RQCMY:68 Stack trace: #0 /in/RQCMY(29): NginxConfigWriter->write(Object(BaseConfigValue)) #1 /in/RQCMY(111): WriteManager->write(Object(BaseConfigValue)) #2 {main} thrown in /in/RQCMY on line 68
Process exited with code 255.

preferences:
261.59 ms | 402 KiB | 330 Q