PHP instance: Implementing a super simple MVC structure

Source: Internet
Author: User
Tags array empty php framework
Here is a super simple MVC architecture implementation, even a data source with a built-in fixed array, although simple, but in fact many of the PHP framework core implementation of the idea should be similar to this

But some frameworks provide more tools for developers to use, I also want to implement a PHP framework, is currently in the planning, but also want to learn from the framework of the development of more PHP design ideas and methods.

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 "<br/>";
}
}


$controller = new Controller ();
$controller->doaction (' Link_page ', ' B ');
$controller->doaction ();


model.php


Code
Class Model {
Private $database = Array (
"A" => "Hello World",
"B" => "OK so Done",
"C" => "Good Bye",
);

//@TODO connect the database
   
   //run the "query and get" result
&nbs p;   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 = "<a href= ' # ' >link a</a>   ';
        $links. = "<a href= ' >link b</a>  ";
        $links. = "<a href= ' >link c</a>  ";
       
        return $links
   }
}

view.php


class View {
   
    Public function display ($output) {
//        ob_start ();
        
        echo $output;
   }
& nbsp;  
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.