Home  >  Article  >  Backend Development  >  PHP mvc mode article system-learning mvc

PHP mvc mode article system-learning mvc

WBOY
WBOYOriginal
2016-07-25 09:05:241154browse
  1. Interface DataOperation
  2. {
  3. public function select($info);
  4. public function selectNum($info);
  5. }
Copy code

The above interface definition reads Data interface, 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.

  1. Interface DataSource
  2. {
  3. public static function getInstance();
  4. }
Copy code

It is assumed here that the database is operated, and DataSource defines an interface, and all those that implement this interface The instance class will get a static object.

  1. Interface Controller
  2. {
  3. public function pop();
  4. public function push();
  5. public function execute();
  6. }
  7. Interface View
  8. {
  9. public function display();
  10. }
Copy the code

Define a class to implement the DataSource interface, using the singleton pattern.

  1. class DataBaseSource implements DataSource
  2. {
  3. public static $instance = null;
  4. public static function getInstance()
  5. {
  6. if(self::$instance == null)
  7. {
  8. self ::$instance == new PDO("mysql:host=localhost;dbname=article","root","123456");
  9. }
  10. return self::$instance;
  11. }
  12. }
Copy code

Define an abstract class to implement DataOperation. We want to share a database connection, so initialize the database object in the abstract class, so that all subclasses can share this object.

  1. abstract class DataBaseOperation implements DataOperation
  2. {
  3. protected $db = null;
  4. public function __construct()
  5. {
  6. $this->db = DataBaseSource::getInstance();
  7. }
  8. public function select($info);
  9. }
Copy code

Let’s write a business subclass to implement the abstract class DataBaseOperation

  1. class Tech extends DataBaseOperation
  2. {
  3. public function select($info)
  4. {
  5. //Implement your code here
  6. }
  7. public function selectNum($info)
  8. {
  9. / /Implement your code here
  10. }
  11. }
Copy the code

After implementing the business logic layer, open the control layer.

  1. class ViewController implements Controller

  2. {
  3. private $mod = array();
  4. public function push($key,$value);
  5. {
  6. //implement you code, register the class into $this->mod;
  7. }
  8. public function pop($key)
  9. {
  10. //Implement your code, set the value of $this->mod[$key] to null;
  11. }
  12. public function execute($key)
  13. {
  14. //Implement your code here and generate an example. Pay attention to using the new features of php5, exception handling
  15. }
  16. }
  17. //Presentation layer, Interface View will be implemented here
  18. abstract ArticleView implements View
  19. {
  20. protected $smarty = null;
  21. public function __construct()
  22. {
  23. $this->smarty = new Smarty();
  24. /// Below you can define some attribute values ​​​​of smarty
  25. }
  26. }
  27. Specific pages, such as the display page of technology articles
  28. class TechArticleView extends ArticleView
  29. {
  30. public function display()
  31. {
  32. //Implement your code, call the Tech class and more DataBaseOperation subclasses
  33. }
  34. }< ;/p>
  35. //General entrance index.php

  36. try
  37. {
  38. $viewController = new ViewController();
  39. $viewController->push("tech",TechArticleView);
  40. //Continuous increase
  41. $mod = $_GET["mod"]:$_GET["mod"]:$_POST["mod"];
  42. //Finally
  43. $viewController->execute($key);
  44. }
  45. catch(Exception $e)
  46. {
  47. //Handling exceptions
  48. }
  49. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn