Home 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;
    }
   
}



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)