Use the MVC mode in php. The MVC mode in php is used for example: a simple article shows that the system is read-only during a simple system, that is to say, this example will not involve the use of MVC in php. First, let me give an example:
A simple article display system
In simple cases, we assume that the article system is read-only. that is to say, this example will not involve the publishing of the article. now it is started.
Since it only involves reading databases, I have defined two interfaces.
Interface DataOperation
{
Public function select ($ info );
Public function selectNum ($ info );
}
The preceding interface defines the interface for reading data. the select method returns the required articles. The selectNum method returns the total number of articles, which is used for pagination. $ Info is an array used to store query conditions.
Interface DataSource
{
Public static function getInstance ();
}
Here we assume that we operate on the database, and DataSource defines an interface. 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 implement it.
The following defines a class to implement the DataSource interface. this class uses the Singleton mode.
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 this 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 );
}
Below 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
}
}
The business logic layer is implemented. 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 $ this-> mod [$ key] to null;
}
Public function execute ($ key)
{
// Implement your code here to generate an instance. use the new features of php5 to handle exceptions.
}
}
Okay. The following is the presentation layer. the Interface View will be implemented here.
Abstract ArticleView implements View
{
Protected $ smarty = null;
Public function _ construct ()
{
$ This-> smarty = new Smarty ();
/// You can define some property values of smarty below
}
}
A specific page, such as a display page of a tech article
Class TechArticleView extends ArticleView
{
Public function display ()
{
// Implement your code, call the Tech class and more DataBaseOperation subclasses
}
}
Okay. The following is the index. php entry.
Try
{
$ ViewController = new ViewController ();
$ ViewController-> push ("tech", TechArticleView );
// Continuous increase
$ Mod = $ _ GET ["mod"]: $ _ GET ["mod"]: $ _ POST ["mod"];
// Last
$ ViewController-> execute ($ key );
}
Catch (Exception $ e)
{
// How to handle exceptions is yours
}
First, let's give an example: when a simple article shows that the system is simple, we assume that the system of this article is read-only. that is to say, this example will not involve the text...