3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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(); }; $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 - 7.1.0
Serving request: http://foo.bar/path/to/resource using Core\Routing\FancyRouter
Output for 5.3.0 - 5.3.29
Fatal error: Class name must be a valid object or a string in /in/h1cGo on line 58
Process exited with code 255.
Output for 5.2.3 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/h1cGo on line 3
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.2
<br /> <b>Parse error</b>: syntax error, unexpected T_STRING in <b>/in/h1cGo</b> on line <b>3</b><br />
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
<br /> <b>Parse error</b>: parse error, unexpected T_STRING in <b>/in/h1cGo</b> on line <b>3</b><br />
Process exited with code 255.
Output for 4.3.2 - 4.3.4
<br /> <b>Parse error</b>: parse error in <b>/in/h1cGo</b> on line <b>3</b><br />
Process exited with code 255.