我对此处的代码量感到抱歉。我试图表达出足够的理解,同时避免混淆(我希望)。我已在第Pastebin列入了代码的第二个副本。 (该代码的执行没有错误/通知/警告。)PHP MVC框架结构
我正在创建一个内容管理系统,同时试图实现模型视图控制器的想法。我最近刚刚遇到了MVC的概念(在上周内),并试图将其实施到我当前的项目中。
CMS的功能之一是动态/可定制菜单区域,每个功能都将由控制器表示。因此会有多个版本的控制器类,每个版本都有特定的扩展功能。
我看了很多教程,并阅读了MVC框架的一些开源解决方案。我现在正在尝试为我的特定要求创建一个轻量级解决方案。我对向后兼容性不感兴趣,我正在使用PHP 5.3。基类的一个优点是不必使用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
我建议你看看http://codeigniter.com/,(如果你没有) – 2010-04-18 04:40:37
我有,这是我的代码基础松散(或至少我的理解)。我看到的一个问题是CodeIgnitor与PHP 4向后兼容,因此使情况复杂化(例如使用&/引用)。我的要求非常具体,认为实施我自己的框架将是有益的(包括学习曲线)。 – bigstylee 2010-04-18 05:18:33