simplified dynamic class instantiation

This commit is contained in:
Steffen Vogel 2010-06-09 21:11:26 +02:00
parent 58240b8ceb
commit 0c162cfede
3 changed files with 16 additions and 20 deletions

View file

@ -28,18 +28,18 @@ final class FrontController {
$response = new HttpResponse();
// create view instance
$rc = new ReflectionClass($request->get['format'] . 'View');
if (!$rc->isSubclassOf('View')) {
throw new InvalidArgumentException('\'' . $rc->getName() . '\' is not a valid View');
$view = $request->get['format'] . 'View';
if (!is_subclass_of($view, 'View')) {
throw new InvalidArgumentException('\'' . $view . '\' is not a valid View');
}
$this->view = $rc->newInstanceArgs(array($request, $response));
$this->view = new $view($request, $response);
// create controller instance
$rc = new ReflectionClass($request->get['controller'] . 'Controller');
if (!$rc->isSubclassOf('Controller')) {
throw new InvalidArgumentException('\'' . $rc->getName() . '\' is not a valid Controller');
$controller = $request->get['controller'] . 'Controller';
if (!is_subclass_of($controller, 'Controller')) {
throw new InvalidArgumentException('\'' . $controller . '\' is not a valid controller');
}
$this->controller = $rc->newInstanceArgs(array($this->view));
$this->controller = new $controller($this->view);
}
public function run() {

View file

@ -157,12 +157,10 @@ abstract class Channel extends DatabaseObject implements ChannelInterface {
* create new channel instance by given database query result
*/
final static protected function factory($object) {
$rc = new ReflectionClass($object['type']);
if (!$rc->isSubclassOf('Channel')) {
throw new InvalidArgumentException('\'' . $rc->getName() . '\' is not a valid channel type');
if (!is_subclass_of($object['type'], 'Channel')) {
throw new InvalidArgumentException('\'' . $object['type'] . '\' is not a valid channel type');
}
return $rc->newInstanceArgs(array($object));
return new $object['type']($object);
}
/*

View file

@ -158,7 +158,7 @@ interface DatabaseInterface {
public function escapeString($string);
public function escape($value);
public function lastInsertId();
}
@ -194,12 +194,10 @@ abstract class Database implements DatabaseInterface {
if (is_null(self::$connection)) {
$config = Registry::get('config');
$rc = new ReflectionClass($config['db']['backend']);
if (!$rc->isSubclassOf('Database')) {
throw new InvalidArgumentException('\'' . $rc->getName() . '\' is not a valid database backend');
if (!is_subclass_of($config['db']['backend'], 'Database')) {
throw new InvalidArgumentException('\'' . $config['db']['backend'] . '\' is not a valid database backend');
}
self::$connection = $rc->newInstanceArgs(array($config['db']));
self::$connection = new $config['db']['backend']($config['db']);
}
return self::$connection;
@ -215,7 +213,7 @@ abstract class Database implements DatabaseInterface {
else {
$value = '\'' . $this->escapeString($value) . '\'';
}
return $value;
}
}