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}\n"; } } 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}\n"; } } // Mock Value objects $nginx_value = new NginxConfigValue(); $base_value = new BaseConfigValue(); $nginx_value->storage_location = $base_value->storage_location = "/some/path"; // Set up manager $manager = new WriteManager(); $manager->addWriter(BaseConfigWriter::class, BaseConfigValue::class); $manager->addWriter(NginxConfigWriter::class, NginxConfigValue::class); $generator = new ExampleGenerator(); $value = $generator->generate(); $manager->write($value); $generator = new NginxGenerator(); $value = $generator->generate(); $manager->write($value);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught TypeError: WriteManager::addWriter(): Argument #1 ($writer) must be of type ConfigWriterInterface, string given, called in /in/MvEsG on line 84 and defined in /in/MvEsG:20 Stack trace: #0 /in/MvEsG(84): WriteManager->addWriter('BaseConfigWrite...', 'BaseConfigValue') #1 {main} thrown in /in/MvEsG on line 20
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
Fatal error: Uncaught TypeError: Argument 1 passed to WriteManager::addWriter() must implement interface ConfigWriterInterface, string given, called in /in/MvEsG on line 84 and defined in /in/MvEsG:20 Stack trace: #0 /in/MvEsG(84): WriteManager->addWriter('BaseConfigWrite...', 'BaseConfigValue') #1 {main} thrown in /in/MvEsG on line 20
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Catchable fatal error: Argument 1 passed to WriteManager::addWriter() must implement interface ConfigWriterInterface, string given, called in /in/MvEsG on line 84 and defined in /in/MvEsG on line 20
Process exited with code 255.

preferences:
155.7 ms | 402 KiB | 183 Q