Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP MVC框架结构_Php_Oop_Model View Controller - Fatal编程技术网

PHP MVC框架结构

PHP MVC框架结构,php,oop,model-view-controller,Php,Oop,Model View Controller,我很抱歉这里的代码太多了。我试图在避免混淆的同时表现出足够的理解力(我希望如此)。我已经在中包含了代码的第二个副本。(代码执行时没有错误/通知/警告。) 我目前正在创建一个内容管理系统,同时尝试实现模型视图控制器的思想。我最近才认识到MVC的概念(在过去的一周内),并试图将其应用到我当前的项目中 CMS的一个功能是动态/可定制菜单区域,每个功能将由一个控制器表示。因此,控制器类将有多个版本,每个版本都具有特定的扩展功能 我已经阅读了许多教程,并阅读了一些MVC框架的开源解决方案。我现在正试图为我

我很抱歉这里的代码太多了。我试图在避免混淆的同时表现出足够的理解力(我希望如此)。我已经在中包含了代码的第二个副本。(代码执行时没有错误/通知/警告。)

我目前正在创建一个内容管理系统,同时尝试实现模型视图控制器的思想。我最近才认识到MVC的概念(在过去的一周内),并试图将其应用到我当前的项目中

CMS的一个功能是动态/可定制菜单区域,每个功能将由一个控制器表示。因此,控制器类将有多个版本,每个版本都具有特定的扩展功能

我已经阅读了许多教程,并阅读了一些MVC框架的开源解决方案。我现在正试图为我的特定需求创建一个轻量级解决方案。我对向后兼容性不感兴趣,我使用的是PHP5.3。基类的一个优点是不必使用
global
,并且可以使用
$this->Obj['ClassName']->property/function()直接访问任何加载的类

希望使用概述的基本结构(考虑性能)获得一些反馈。具体而言
a) 我是否正确理解/实施了MVC的概念?
b) 我是否正确理解/使用PHP5实现了面向对象的技术?
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

作为PHP的业余用户,我通过阅读MVC PHP框架的源代码和web上的文章来学习PHP

在我看来,没有(或很少)开源框架实现“超级类”的思想,相反,它们使用PHP的自动加载功能:


此外,从我在文章和书籍中所学到的,据说使用“超级课堂”是一种不好的做法

我认为您可以通过实现自动加载来改进代码

您也可以在www.kohanaphp.com上查看Kohana。
Kohana 2系列是一款仅限PHP-5的CodeIgniter重写和改进,

Kohana 3系列是一个全新的框架,具有不同的体系结构。

作为PHP的业余用户,我通过阅读MVC PHP框架的源代码和web上的文章来学习PHP

在我看来,没有(或很少)开源框架实现“超级类”的思想,相反,它们使用PHP的自动加载功能:


此外,从我在文章和书籍中所学到的,据说使用“超级课堂”是一种不好的做法

我认为您可以通过实现自动加载来改进代码

您也可以在www.kohanaphp.com上查看Kohana。
Kohana 2系列是一款仅限PHP-5的CodeIgniter重写和改进,

虽然Kohana 3系列是一个全新的框架,具有不同的体系结构。

我建议您看看(如果您没有)我有,这就是我的代码松散的基础(或者至少是我的理解)。我看到的一个问题是CodeIgnitor向后兼容PHP4,因此使情况过于复杂(例如使用&/引用)。我的要求非常具体,我认为实现我自己的框架将是有益的(包括学习曲线)。我建议您看看(如果您没有)我有,这就是我的代码松散的基础(或至少是我的理解)。我看到的一个问题是CodeIgnitor向后兼容PHP4,因此使情况过于复杂(例如使用&/引用)。我的要求非常具体,我认为实施我自己的框架将是有益的(包括学习曲线)。