3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Core\Routing { interface Router { public function serve($uri); } class SimpleRouter implements Router { public function serve($uri) { echo 'Serving request: ', $uri, ' using ', __CLASS__; } } class FancyRouter implements Router { public function serve($uri) { echo 'Serving request: ', $uri, ' using ', __CLASS__; } } } namespace Core\Database { interface Connection { public function query($sql); } class MySQLConnection implements Connection { public function query($sql) { echo 'Querying: ', $sql, ' using ', __CLASS__; } } class MariaDBConnection implements Connection { public function query($sql) { echo 'Querying: ', $sql, ' using ', __CLASS__; } } } namespace Core { abstract class Application { protected static $instances = array(); private $properties = array(); public static function getInstance() { return isset(self::$instances[$className = get_called_class()]) ? self::$instances[$className] : self::$instances[$className] = new static(); } protected function __construct() { } public function __set($propertyName, $value) { $this->properties[$propertyName] = $value; } public function __get($propertyName) { return is_callable($this->properties[$propertyName]) ? $this->properties[$propertyName] = $this->properties[$propertyName]($this) : $this->properties[$propertyName]; } } abstract class WebApplication extends Application { protected $routerClassName = '\\Core\\Routing\\SimpleRouter'; // TODO: why not use constants here ??? maybe code should be allowed to modify dependencies at runtime ??? protected $databaseConnectionClassName = '\\Core\\Database\\MySQLConnection'; protected function __construct() { $this->router = function ($application) { //$routerClassName = $application->routerClassName; //return new $routerClassName(); return new $application->routerClassName(); }; $this->databaseConnection = function ($application) { $databaseConnectionClassName = $application->databaseConnectionClassName; return new $databaseConnectionClassName(); }; } } } namespace CompanyName { final class DiscussionsWebApplication extends \Core\WebApplication { protected $routerClassName = '\\Core\\Routing\\FancyRouter'; protected $databaseConnectionClassName = '\\Core\\Database\\MariaDBConnection'; } } namespace { CompanyName\DiscussionsWebApplication::getInstance()->router->serve('http://foo.bar/path/to/resource'); }
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0 - 7.2.33, 7.3.12 - 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.6
Serving request: http://foo.bar/path/to/resource using Core\Routing\FancyRouter
Output for 5.3.0 - 5.3.29
Notice: Undefined index: routerClassName in /in/JkXke on line 48 Notice: Undefined index: routerClassName in /in/JkXke on line 48 Fatal error: Class name must be a valid object or a string in /in/JkXke on line 59
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/JkXke on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/JkXke on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/JkXke on line 3
Process exited with code 255.

preferences:
207.58 ms | 401 KiB | 354 Q