Advertisement
Guest User

Untitled

a guest
Aug 8th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.27 KB | None | 0 0
  1. class DB
  2. {
  3.     private static $_instance;
  4.     private $pdo;
  5.  
  6.     private $dbUser = 'hellomoto';
  7.     private $dbPassword = 'pass';
  8.     private $dbName = 'training';
  9.     private $dbHost = 'localhost';
  10.  
  11.     private function __construct() {
  12.         try {
  13.             $h = new PDO("mysql:host=$this->dbHost;dbname=$this->dbName", $this->dbUser, $this->dbPassword);
  14.         } catch (PDOExeption $e) {
  15.             die($e->getMessage());
  16.         }
  17.         $this->pdo = $h;
  18.     }
  19.  
  20.     static protected function getInstance() {
  21.         if (self::$_instance === null) {
  22.             self::$_instance = new self();
  23.         }
  24.         return self::$_instance;
  25.     }
  26.  
  27.     private function __clone() {}
  28.     private function __wakeup() {}
  29.  
  30.     public function Select($query) {
  31.         $stmt = $this->pdo->query($query);
  32.         $result = $stmt->fetch(PDO::FETCH_ASSOC);
  33.         return $result;
  34.     }
  35.  
  36. }
  37.  
  38. class ArticlesModel extends DB
  39. {
  40.  
  41.     private static $_instance;
  42.     private $msql;
  43.  
  44.     static public function getInstance() {
  45.         if (self::$_instance === null) {
  46.             self::$_instance = new self();
  47.         }
  48.         return self::$_instance;
  49.     }  
  50.  
  51.     public function __construct() {
  52.         $this->msql = DB::getInstance();
  53.     }
  54.  
  55.     public function articlesAll() {
  56.         $query = "SELECT * FROM Articles";
  57.         $result = $this->msql->Select($query);
  58.         return $result;
  59.     }
  60. }
  61.  
  62. $art = ArticlesModel::getInstance()->articlesAll();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement