Phpmvc-learning mvc

Source: Internet
Author: User
Phpmvc-learning mvc

  1. Interface DataOperation
  2. {
  3. Public function select ($ info );
  4. Public function selectNum ($ info );
  5. }

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.

  1. Interface DataSource
  2. {
  3. Public static function getInstance ();
  4. }

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.

  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. }

Defines a class to implement the DataSource interface and uses the Singleton mode.

  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. }

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.

  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. }

Next, 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. }

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 your code and register the class into $ this-> mod;
  7. }
  8. Public function pop ($ key)
  9. {
  10. // Implement your code and set $ this-> mod [$ key] to null;
  11. }
  12. Public function execute ($ key)
  13. {
  14. // Implement your code here to generate an instance. use the new features of php5 to handle exceptions.
  15. }
  16. }
  17. // Presentation layer, which implements the Interface View
  18. Abstract ArticleView implements View
  19. {
  20. Protected $ smarty = null;
  21. Public function _ construct ()
  22. {
  23. $ This-> smarty = new Smarty ();
  24. /// You can define some property values of smarty below
  25. }
  26. }
  27. A specific page, such as a display page of a tech article
  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. }

  35. // Total entry 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. // Last
  43. $ ViewController-> execute ($ key );
  44. }
  45. Catch (Exception $ e)
  46. {
  47. // Handle exceptions
  48. }
  49. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.