<?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 git.master, git.master_jit, rfc.property-hooks
- Serving request: http://foo.bar/path/to/resource using Core\Routing\FancyRouter
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:
45.89 ms | 401 KiB | 8 Q