【发布时间】:2010-04-18 04:02:53
【问题描述】:
我很抱歉这里的代码量。我试图在避免混淆的同时表现出足够的理解(我希望如此)。我在Pastebin 包含了代码的第二个副本。 (代码在没有错误/通知/警告的情况下执行。)
我目前正在创建一个内容管理系统,同时尝试实现模型视图控制器的想法。我最近才(上周)遇到了 MVC 的概念,并试图将其实施到我当前的项目中。
CMS 的功能之一是动态/可自定义的菜单区域,每个功能都将由一个控制器表示。因此会有多个版本的控制器类,每个版本都有特定的扩展功能。
我查看了许多教程并阅读了一些 MVC 框架的开源解决方案。我现在正在尝试为我的特定要求创建一个轻量级的解决方案。我对向后兼容性不感兴趣,我使用的是 PHP 5.3。 Base 类的一个优点是不必使用global,并且可以使用$this->Obj['ClassName']->property/function(); 直接访问任何加载的类。
希望使用概述的基本结构(考虑到性能)获得一些反馈。具体来说;
a) 我是否正确理解/实现了 MVC 的概念?
b) 我是否正确理解/实施了使用 PHP 5 的面向对象技术?
c) Base 的类属性应该是静态的吗?
d) 改进?
非常感谢您!
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
/* A "Super Class" that creates instances */
class Base {
public static $Obj = array(); // Not sure this is the correct use of the "static" keyword?
public static $var;
static public function load_class($directory, $class)
{
echo count(self::$Obj)."\n"; // This does show the array is getting updated and not creating a new array :)
if (!in_array($class, self::$Obj)) //dont want to load it twice
{
/* Locate and include the class file based upon name ($class) */
return self::$Obj[$class] = new $class();
}
return TRUE;
}
}
/* Loads general configuration objects into the "Super Class" */
class Libraries extends Base {
public function __construct(){
$this->load_class('library', 'Database');
$this->load_class('library', 'Session');
self::$var = 'Hello World!'; //testing visibility
/* Other general funciton classes */
}
}
class Database extends Base {
/* Connects to the the database and executes all queries */
public function query(){}
}
class Session extends Base {
/* Implements Sessions in database (read/write) */
}
/* General functionality of controllers */
abstract class Controller extends Base {
protected function load_model($class, $method) {
/* Locate and include the model file */
$this->load_class('model', $class);
call_user_func(array(self::$Obj[$class], $method));
}
protected function load_view($name) {
/* Locate and include the view file */
#include('views/'.$name.'.php');
}
}
abstract class View extends Base { /* ... */ }
abstract class Model extends Base { /* ... */ }
class News extends Controller {
public function index() {
/* Displays the 5 most recent News articles and displays with Content Area */
$this->load_model('NewsModel', 'index');
$this->load_view('news', 'index');
echo self::$var;
}
public function menu() {
/* Displays the News Title of the 5 most recent News articles and displays within the Menu Area */
$this->load_model('news/index');
$this->load_view('news/index');
}
}
class ChatBox extends Controller { /* ... */ }
/* Lots of different features extending the controller/view/model class depending upon request and layout */
class NewsModel extends Model {
public function index() {
echo self::$var;
self::$Obj['Database']->query(/*SELECT 5 most recent news articles*/);
}
public function menu() { /* ... */ }
}
$Libraries = new Libraries;
$controller = 'News'; // Would be determined from Query String
$method = 'index'; // Would be determined from Query String
$Content = $Libraries->load_class('controller', $controller); //create the controller for the specific page
if (in_array($method, get_class_methods($Content)))
{
call_user_func(array($Content, $method));
}
else
{
die('Bad Request'. $method);
}
$Content::$var = 'Goodbye World';
echo $Libraries::$var . ' - ' . $Content::$var;
?>
/* Output */
0
1
2
3
Hello World!Hello World!Goodbye World - Goodbye World
【问题讨论】:
-
我建议你看看codeigniter.com,(如果你没有)
-
我有,这就是我的代码松散的基础(或至少我的理解)。我看到的问题之一是 CodeIgnitor 向后兼容 PHP 4,因此使情况变得过于复杂(例如,使用 &/references)。我的要求非常具体,并且认为实施我自己的框架会很有好处(包括学习曲线)。
标签: php oop model-view-controller