Guest User

Untitled

a guest
Sep 13th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. PHP MVC from the scratch - How to connect these classes?
  2. <?php
  3. // filename: "config.php"
  4.  
  5. // another includes or requires here
  6.  
  7. class Config
  8. {
  9. // begin data section
  10. public static $UserName;
  11. public static $UserPassword;
  12. // end data section
  13.  
  14. // begin singleton section
  15. private static $_instance;
  16.  
  17. public static function getInstance()
  18. {
  19. if (!self::$_instance instanceof self)
  20. {
  21. self::$_instance = new self;
  22. }
  23. return self::$_instance;
  24. }
  25. // end singleton section
  26.  
  27. // start session section
  28. public static function SaveSession()
  29. {
  30. $_SESSION['config_username'] = Config::$UserName;
  31. $_SESSION['config_password'] = Config::$UserPassword;
  32. }
  33.  
  34. public static function RestoreSession()
  35. {
  36. Config::$UserName = $_SESSION['config_username'];
  37. Config::$UserPassword = $_SESSION['config_password'];
  38. }
  39. // end session section
  40.  
  41. } // class Config
  42.  
  43. ?>
  44.  
  45. <?php
  46. // filename: "index.php"
  47.  
  48. include ("config.php");
  49. // another includes or requires here
  50.  
  51. class Application
  52. {
  53. public static function main()
  54. {
  55. // prepare session variables
  56. session_start();
  57.  
  58. // prepare singletons here
  59. $instance = Config::getInstance();
  60.  
  61. // this code its an example
  62. $instance->UserName = "johndoe";
  63. $instance->UserPassword = "123";
  64. $instance->SaveSession();
  65.  
  66. // all the page construction goes here
  67. // ...
  68. }
  69. } //
  70.  
  71. // program starts here:
  72. Application::main();
  73.  
  74. ?>
  75.  
  76. <?php
  77. // filename: "customers.php"
  78.  
  79. include ("config.php");
  80. // another includes or requires here
  81.  
  82. class Application
  83. {
  84. public static function main()
  85. {
  86. // copy data from session to singletons
  87. $instance->RestoreSession();
  88.  
  89. // all the page construction goes here
  90. // ...
  91. }
  92. } //
  93.  
  94. // program starts here:
  95. Application::main();
  96.  
  97. ?>
Add Comment
Please, Sign In to add comment