时间:2021-07-01 10:21:17 帮助过:5人阅读
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
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;
}
}
本文转自:http://hi.baidu.com/ncsittiantian/item/72a30f081ec27edcdde5b036