首页 php教程 php手册 PHP实例:实现超级简单的MVC结构

PHP实例:实现超级简单的MVC结构

Jun 21, 2016 am 08:56 AM

下面是一个超级简单的MVC结构实现,甚至连数据源都用了一个内置的固定数组,虽然简单,但其实众多的PHP Framework核心实现的思想应该和这个是差不多的

只不过一些framework提供了更多的方便开发者使用的工具,我也想自己来实现一个PHP的 框架,目前正在着手策划中,也希望自己能够从框架的开发中学习到更多的PHP设计思想和方法。

Controller.php

include 'Model.php';
include 'View.php';

class Controller {
    private $model     = '';
    private $view     = '';
   
    public function Controller(){
        $this->model    =    new Model();
        $this->view        =    new View();
    }
   
    public function doAction( $method = 'defaultMethod', $params = array() ){
        if( empty($method) ){
            $this->defaultMethod();
        }else if( method_exists($this, $method) ){
            call_user_func(array($this, $method), $params);
        }else{
            $this->nonexisting_method();
        }
    }
   
    public function link_page($name = ''){
        $links = $this->model->getLinks();
        $this->view->display($links);
       
        $result = $this->model->getResult($name);
        $this->view->display($result);
    }
   
    public function defaultMethod(){
        $this->br();
        echo "This is the default method. ";
    }
   
    public function nonexisting_method(){
        $this->br();
        echo "This is the noexisting method. ";
    }
   
    public function br(){
        echo "
";
    }
}


$controller = new Controller();
$controller->doAction('link_page', 'b');
$controller->doAction();


Model.php


Code
class Model {
    private $database = array(
        "a"    =>    "hello world",
        "b"    =>    "ok well done",
        "c"    =>    "good bye",
    );
   
    //@TODO connect the database
   
    //run the query and get the result
    public function getResult($name){
        if( empty($name) ){
            return FALSE;
        }
       
        if( in_array($name, array_keys( $this->database ) ) ){
            return $this->database[$name];
        }
    }

    public function getLinks(){
        $links = "Link A  ";
        $links.= "Link B  ";
        $links.= "Link C  ";
       
        return $links;
    }
}

View.php


class View {
   
    public function display($output){
//        ob_start();
       
        echo $output;
    }
   
}



本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)