3v4l.org

run code in 300+ PHP versions simultaneously
<?php /********************** OBJECTS **********************/ interface OldInterface {} class OldA implements OldInterface {} class OldB implements OldInterface {} interface NewInterface {} class NewA implements NewInterface {} class NewB implements NewInterface {} /********************** CONVERTERS **********************/ interface ConverterInterface { public function supports(OldInterface $old): bool; public function convert(OldInterface $old): NewInterface; } class AConverter implements ConverterInterface { public function supports(OldInterface $old): bool { return $old instanceof OldA; } public function convert(OldInterface $old): NewInterface { return new NewA(); } } class BConverter implements ConverterInterface { public function supports(OldInterface $old): bool { return $old instanceof OldB; } public function convert(OldInterface $old): NewInterface { return new NewB(); } } /********************** CONVERTER DISCOVERER **********************/ interface ConverterDiscovererInterface { public function discoverConverterFor(OldInterface $old): ?ConverterInterface; } class ConverterDiscoverer implements ConverterDiscovererInterface { /** * @param ConverterInterface[] $converters */ public function __construct(private array $converters) {} public function discoverConverterFor(OldInterface $old): ?ConverterInterface { foreach ($this->converters as $converter) { if ($converter->supports($old)) { return $converter; } } return null; } } /********************** MASS CONVERTER **********************/ class MassConverter { public function __construct(private ConverterDiscovererInterface $converterDiscoverer) {} /** * @param OldInterface[] $oldObjects * @return NewInterface[] */ public function massConvert(array $oldObjects): array { $newObjects = []; foreach ($oldObjects as $oldObject) { $converter = $this->converterDiscoverer->discoverConverterFor($oldObject); if ($converter !== null) { $newObjects[] = $converter->convert($oldObject); } } return $newObjects; } } /********************** SAMPLE **********************/ $converterDiscoverer = new ConverterDiscoverer([new AConverter(), new BConverter()]); $massConverter = new MassConverter($converterDiscoverer); var_dump($massConverter->massConvert([new OldA(), new OldB()]));
Output for 8.0.0 - 8.0.3, 8.1.23 - 8.1.33, 8.2.10 - 8.2.29, 8.3.0 - 8.3.4, 8.3.6 - 8.3.26, 8.4.1 - 8.4.14
array(2) { [0]=> object(NewA)#7 (0) { } [1]=> object(NewB)#8 (0) { } }
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 array(2) { [0]=> object(NewA)#7 (0) { } [1]=> object(NewB)#8 (0) { } }
Output for 7.3.0 - 7.3.28, 7.4.0 - 7.4.16
Parse error: syntax error, unexpected 'private' (T_PRIVATE), expecting variable (T_VARIABLE) in /in/6KOo4 on line 59
Process exited with code 255.

preferences:
97.52 ms | 409 KiB | 5 Q