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 git.master, git.master_jit, rfc.property-hooks
Fatal error: Cannot declare class XMLParser, because the name is already in use in /in/ukJtE on line 73
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
163.37 ms | 405 KiB | 5 Q