Thank you for your input, kyberfabrikken.
I made some changes:
- The class names of the models are now plural and without '_Model'.
- The model classes extend the class Base_Model, which doesnīt do anything yet. But itīs there for a reason.
- Thereīs a new class Registry. I donīt know if "Registry" is the proper term. It basically loads all model classes. It also could be used to provide other things like language or config data.
- When a controller is loaded all models are available in it. That means Iīm now able to use one, more or no models in a controller.
BTW: The header() function at the beginning is there just during development. I think itīs better to decide inside a contoller how the output should look like. I still have to find a good way to do this.
PHP Code:
class Base_Model {
// do something for all models, e.g. database stuff
}
class Users extends Base_Model {
private $user = array('name' => 'oerdec', 'email' => 'oerdec@example.com');
public function getUser() {
return $this->user;
}
}
class Admins extends Base_Model {
private $admin = array('name' => 'monitormensch', 'email' => 'monitormensch@example.com');
public function getAdmin() {
return $this->admin;
}
}
class User_Controller extends Application {
protected function displayUser() {
$data = $this->users->getUser();
foreach ($data as $key => $value) {
$modified_data[$key] = strtoupper($value);
}
return $this->tpl->render('tpl_user.php', $modified_data);
}
protected function displayParams($first, $second) {
$params = array('first' => $first, 'second' => $second);
return $this->tpl->render('tpl_params.php', $params);
}
protected function registryTest() {
return $this->admins->getAdmin();
}
}
class Registry {
private $registered_models = array();
private function getDeclaredModels() {
$classes = get_declared_classes();
foreach ($classes as $class) {
if (is_subclass_of($class, 'Base_Model')) {
$models[] = $class;
}
}
return isset($models) ? $models : FALSE;
}
public function registerModels() {
$models = $this->getDeclaredModels();
if ($models !== FALSE) {
foreach ($models as $model) {
$this->registered_models[] = new $model;
}
}
}
public function getRegisteredModels() {
return !empty($this->registered_models) ? $this->registered_models : FALSE;
}
}
class Template {
public function render($tpl_file, $data) {
extract($data);
ob_start();
include($tpl_file);
return ob_get_clean();
}
}
class Application {
private $controller;
private $models = FALSE;
protected $tpl;
public function loadModels($models) {
$this->models = $models;
}
public function loadController($controller) {
$this->controller = new $controller;
$this->controller->tpl = new Template;
if ($this->models !== FALSE) {
foreach ($this->models as $model) {
$class_name = strtolower(get_class($model));
$this->controller->$class_name = $model;
}
}
}
public function runAction() {
$params = func_get_args();
$action = $params[0];
$num_params = sizeof($params);
if ($num_params >= 2) {
unset($params[0]);
return call_user_func_array(array($this->controller, $action), $params);
}
else {
return $this->controller->$action();
}
}
}
// Test
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
$registry = new Registry;
$registry->registerModels();
$registered_models = $registry->getRegisteredModels();
$application = new Application();
$application->loadModels($registered_models);
$application->loadController('User_Controller');
echo "User_Controller::displayUser():\n\n";
print_r($application->runAction('displayUser'));
echo "User_Controller::displayParams():\n\n";
print_r($application->runAction('displayParams', 'one', 'two'));
echo "User_Controller::registryTest():\n\n";
print_r($application->runAction('registryTest'));
Bookmarks