3v4l.org

run code in 300+ PHP versions simultaneously
<?php //source: https://dzone.com/articles/simple-example-illustrates //FIX-ME !!!!!!!!!!!!! class Parser { private $successor; public function __construct(Parser $successor) { $this->setSuccessor($successor); } public function parse(string $fileName): void { if ($this->getSuccessor() != null) { $this->getSuccessor()->parse($fileName); } else { echo "Unable to find the correct parser for the file:" . $fileName . PHP_EOL; } } protected function canHandleFile(string $fileName, string $format): bool { $spl = new SplFileInfo($fileName); return ($fileName == null) || ($spl->getExtension() == $format); } public function getSuccessor(): ?Parser { return $this->successor; } public function setSuccessor(Parser $successor): void { $this->successor = $successor; } } class TextParser extends Parser { public function parse(string $fileName): void { if ($this->canHandleFile($fileName, "txt")) { echo("A text parser is handling the file: " . $fileName . PHP_EOL); } else { parent::parse($fileName); } } } class JsonParser extends Parser { public function parse(string $fileName): void { if ($this->canHandleFile($fileName, "json")) { echo("A JSON parser is handling the file: " . $fileName . PHP_EOL); } else { parent::parse($fileName); } } } class CSVParser extends Parser { public function parse(string $fileName): void { if ($this->canHandleFile($fileName, "json")) { echo("A CSV parser is handling the file: " + $fileName . PHP_EOL); } else { parent::parse($fileName); } } } class XMLParser extends Parser { public function __construct() { return; } public function parse(string $fileName): void { if ($this->canHandleFile($fileName, "json")) { echo("A XML parser is handling the file: " + $fileName . PHP_EOL); } else { parent::parse($fileName); } } } //List of file names to parse. $fileList = ["someFile.txt", "otherFile.json", "xmlFile.xml", "csvFile.csv", "csvFile.doc"]; //No successor for this handler because this is the last in chain. $xmlParser = new XmlParser(); //XmlParser is the successor of CsvParser. $csvParser = new CsvParser($xmlParser); //CsvParser is the successor of JsonParser. $jsonParser = new JsonParser($csvParser); //JsonParser is the successor of TextParser. //TextParser is the start of the chain. $textParser = new TextParser($jsonParser); //Pass the file name to the first handler in the chain. foreach ($fileList as $key => $fileName) { $textParser->parse($fileName); }
Output for 8.5.0
Fatal error: Cannot redeclare class XMLParser in /in/ukJtE on line 73 Stack trace: #0 {main}
Process exited with code 255.
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 8.4.1 - 8.4.14
Fatal error: Cannot redeclare class XMLParser in /in/ukJtE on line 73
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28
Fatal error: Cannot declare class XMLParser, because the name is already in use in /in/ukJtE on line 73
Process exited with code 255.
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
A text parser is handling the file: someFile.txt A JSON parser is handling the file: otherFile.json Unable to find the correct parser for the file:xmlFile.xml Unable to find the correct parser for the file:csvFile.csv Unable to find the correct parser for the file:csvFile.doc

preferences:
159.8 ms | 413 KiB | 5 Q