<?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.3.0 - 5.3.28, 5.4.0 - 5.4.24
- Parse error: syntax error, unexpected '{' in /in/GAE3l on line 59
Process exited with code 255.
preferences:
178.98 ms | 1395 KiB | 61 Q