3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class BranchDependency { private $_dependencies = []; private $_successors = []; private $_successful = false; public final function hasSuccessor(string $name) { return array_key_exists($name, $this->getSuccessors()); } public final function hasDependency(string $name) { return array_key_exists($name, $this->getDependencies()); } public final function getName() { return static::class; } public final function addDependency(BranchDependency $state) { $name = $state->getName(); if (!$this->hasDependency($name)) { $this->_dependencies[$name] = $state; $state->addSuccessor($this); } } public final function getDependencies() { return $this->_dependencies; } public final function addSuccessor(BranchDependency $state) { $name = $state->getName(); if (!$this->hasSuccessor($name)) { $this->_successors[$name] = $state; $state->addDependency($this); } } public final function getSuccessors() { return $this->_successors; } public final function wasSuccessful() { return $this->_successful; } public final function commit() { if ($this->accomplish()) { foreach ($this->getSuccessors() as $successor) { $successor->commit(); } } } private function accomplish() { foreach ($this->getDependencies() as $dependency) { if (!$dependency->wasSuccessful()) { return false; } } $this->_successful = $this->execute(); return $this->wasSuccessful(); } abstract protected function execute(); } final class BranchDependencyFactory { private static $instance = null; private $_instances = []; private function __construct() { } public static function Instance() { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } public function exists(string $name) { return array_key_exists($name, $this->_instances); } public function getUniqueInstance(string $name) { if (!$this->exists($name)) { $this->_instances[$name] = new $name(); } return $this->_instances[$name]; } } final class Internal extends BranchDependency { public function __construct(bool $internal) { if ($internal) { $one = BranchDependencyFactory::Instance()->getUniqueInstance(One::class); $this->addSuccessor($one); } $two = BranchDependencyFactory::Instance()->getUniqueInstance(Two::class); $this->addSuccessor($two); } public function execute() { return true; } } final class One extends BranchDependency { protected function execute() { print '#1' . PHP_EOL; return true; } } final class Two extends BranchDependency { public function __construct() { $three = BranchDependencyFactory::Instance()->getUniqueInstance(Three::class); $four = BranchDependencyFactory::Instance()->getUniqueInstance(Four::class); $this->addSuccessor($three); $this->addSuccessor($four); } protected function execute() { print '#2' . PHP_EOL; return true; } } final class Three extends BranchDependency { public function __construct() { $five = BranchDependencyFactory::Instance()->getUniqueInstance(Five::class); $this->addSuccessor($five); } protected function execute() { print '#3' . PHP_EOL; return true; } } final class Four extends BranchDependency { public function __construct() { $five = BranchDependencyFactory::Instance()->getUniqueInstance(Five::class); $this->addSuccessor($five); } protected function execute() { print '#4' . PHP_EOL; return false; } } final class Five extends BranchDependency { protected function execute() { print '#5' . PHP_EOL; return true; } } $internal = new Internal(false); $internal->commit();
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
#2 #3 #4
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 #2 #3 #4
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
Catchable fatal error: Argument 1 passed to Internal::__construct() must be an instance of bool, boolean given, called in /in/OsWR7 on line 205 and defined in /in/OsWR7 on line 115
Process exited with code 255.

preferences:
239.53 ms | 402 KiB | 224 Q