Using MVC pattern in php
First let me give you an example:
A simple article display system
For simplicity, we assume that this article system is read-only, which means that this example will not involve the publication of articles, and we will start now.
Since it only involves reading from
database, I defined two interfaces
Interface DataOperation
{
Public function select($info);
Public function selectNum($info);
}
The above interface defines the interface for reading data, and the select method will return the required articles. The selectNum method returns the total number of articles, which is used for paging display. $info is an array used to store query conditions
Interface DataSource
{
Public static function getInstance();
}
Here we assume that we are operating a database, DataSource defines an interface, and all instance classes that implement this interface will get a static object
Interface Controller
{
Public function pop();
Public function push();
Public function execute();
}
Interface View
{
Public function display();
}
Okay, let’s make it happen.
Define a class below to implement the DataSource interface. This class uses the singleton pattern
class DataBaseSource implements DataSource
{
Public static $instance = null;
Public static function getInstance()
{
if(self::$instance == null)
{
self::$instance == new PDO("mysql:host=localhost;dbname=article","root","123456");
}
return self::$instance;
}
}
Define an abstract class to implement DataOperation. We need to share a database connection, so I initialize the database object in the abstract class so that all subclasses can share this object
abstract class DataBaseOperation implements DataOperation
{
protected $db = null;
Public function __construct()
{
$this->db = DataBaseSource::getInstance();
}
Public function select($info);
Public function select($info);
}
Next I will write a business subclass to implement the abstract class DataBaseOperation
class Tech extends DataBaseOperation
{
Public function select($info)
{
//Implement your code here
}
Public function selectNum($info)
{
//Implement your code here
}
}
We have implemented the business logic layer, and the following is the control layer
class ViewController implements Controller
{
private $mod = array();
Public function push($key,$value);
{
//Implement your code and register the class into $this->mod;
}
Public function pop($key)
//Implement your code and set the value of $this->mod[$key] to null;
}
Public function execute($key)
{
//Implement your code here and generate examples. Pay attention to using the new features of php5 and exception handling
}
}
Okay, here is the presentation layer, where Interface View
will be implemented
abstract ArticleView implements View
{
protected $smarty = null;
Public function __construct()
{
$this->smarty = new Smarty();
/// Below you can define some attribute values of smarty
}
}
Specific pages, such as the display page of scientific and technological articles
class TechArticleView extends ArticleView
{
Public function display()
{
//Implement your code and call the Tech class and more DataBaseOperation subclasses
}
}
Okay, here is the main entrance index.php
try
{
$viewController = new ViewController();
$viewController->push("tech",TechArticleView);
//continuous increase
$mod = $_GET["mod"]:$_GET["mod"]:$_POST["mod"];
//Finally
$viewController->execute($key);
}
catch(Exception $e)
{
//How to handle exceptions is your business
}
http://www.bkjia.com/PHPjc/631903.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631903.htmlTechArticleUsing the MVC pattern in php First, let me give an example: A simple article display system During the simple period, we assume This article system is read-only, which means that this example will not involve the article...