Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. <?php
  2.  
  3. class App
  4. {
  5. protected $dbLink;
  6. protected $dbConfig = array(
  7. 'host' => 'localhost',
  8. 'user' => 'user',
  9. 'pass' => 'password',
  10. 'name' => 'mydatabase'
  11. );
  12.  
  13. public function getDbAdapter()
  14. {
  15. if (is_null($this->dbLink)) {
  16. $this->dbLink = new dbAdapter(
  17. $this->dbConfig
  18. );
  19. }
  20.  
  21. return $this->dbLink;
  22. }
  23. }
  24.  
  25. class dbAdapter
  26. {
  27. protected $dbHost = '';
  28. protected $dbUser = '';
  29. protected $dbPass = '';
  30. protected $dbName = '';
  31. protected $link;
  32.  
  33. public function __construct($config = array())
  34. {
  35. $this->dbHost = $config['host'];
  36. $this->dbUser = $config['user'];
  37. $this->dbPass = $config['pass'];
  38. $this->dbName = $config['name'];
  39. }
  40.  
  41. public function getLink()
  42. {
  43. if (is_null($this->link)) {
  44. $this->link = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName);
  45. if (mysqli_error($this->link)) {
  46. throw new Exception(mysqli_error($this->link));
  47. }
  48. if ($this->link) {
  49. mysqli_set_charset($this->link, "utf8");
  50. }
  51. }
  52. return $this->link;
  53. }
  54.  
  55. public function runQuery($query)
  56. {
  57. return mysqli_query($this->getLink(), $query);
  58. }
  59. }
  60.  
  61. final class Core
  62. {
  63. static private $_app;
  64.  
  65. public static function app()
  66. {
  67. if (null === self::$_app) {
  68. self::$_app = new App();
  69. }
  70.  
  71. return self::$_app;
  72. }
  73. }
  74.  
  75. abstract class Model
  76. {
  77. protected $dbAdapter;
  78.  
  79. public function __construct()
  80. {
  81. $this->dbAdapter = Core::app()->getDbAdapter();
  82. }
  83.  
  84. public function load()
  85. {
  86. $this->beforeLoad();
  87. $this->_load();
  88. $this->afterLoad();
  89.  
  90. return $this;
  91. }
  92.  
  93. protected function beforeLoad()
  94. {
  95. return $this;
  96. }
  97.  
  98. protected function afterLoad()
  99. {
  100. return $this;
  101. }
  102.  
  103. protected function _load()
  104. {
  105. return $this;
  106. }
  107. }
  108.  
  109. class User
  110. {
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement