- 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 ();
- }
It is assumed that the operation is performed 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 ();
- }
Defines a class to implement the DataSource interface and 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. Therefore, 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 );
- }
Next, 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
- }
- }
After implementing the business logic layer, open 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.
- }
- }
- // Presentation layer, which implements the Interface View
- 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
- }
- }
-
// Total entry index. php
- Try
- {
- $ ViewController = new ViewController ();
- $ ViewController-> push ("tech", TechArticleView );
- // Continuous increase
- $ Mod = $ _ GET ["mod"]: $ _ GET ["mod"]: $ _ POST ["mod"];
- // Last
- $ ViewController-> execute ($ key );
- }
- Catch (Exception $ e)
- {
- // Handle exceptions
- }
- ?>
|