3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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 BaseConfigWriter implements ConfigWriterInterface { public function write(BaseConfigValue $value) { echo "writing to {$value->storage_location}"; } } class ExampleGenerator { public function generate() { $value = new BaseConfigValue(); $value->storage_location = "/some/path/sample.config"; return $value; } } // Set up manager $manager = new WriteManager(); $manager->addWriter(new BaseConfigWriter, BaseConfigValue::class); $generator = new ExampleGenerator(); $value = $generator->generate(); $manager->write($value);
based on RpMVW
Output for 7.0.0 - 7.3.0rc4
Fatal error: Declaration of BaseConfigWriter::write(BaseConfigValue $value) must be compatible with ConfigWriterInterface::write(ConfigValueInterface $value) in /in/M9jR9 on line 47
Process exited with code 255.
Output for 5.5.24 - 5.6.28
Fatal error: Declaration of BaseConfigWriter::write() must be compatible with ConfigWriterInterface::write(ConfigValueInterface $value) in /in/M9jR9 on line 48
Process exited with code 255.
Output for 5.4.10 - 5.4.45
Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /in/M9jR9 on line 71
Process exited with code 255.
Output for 5.3.20 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/M9jR9 on line 18
Process exited with code 255.