34 lines
711 B
PHP
34 lines
711 B
PHP
|
<?php
|
||
|
|
||
|
namespace Core;
|
||
|
|
||
|
use PDO;
|
||
|
use PDOException;
|
||
|
|
||
|
abstract class Model
|
||
|
{
|
||
|
protected $db;
|
||
|
|
||
|
public function __construct()
|
||
|
{
|
||
|
$this->db = $this->getConnection();
|
||
|
}
|
||
|
|
||
|
private function getConnection()
|
||
|
{
|
||
|
static $connection = null;
|
||
|
|
||
|
if ($connection === null) {
|
||
|
$dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8';
|
||
|
try {
|
||
|
$connection = new PDO($dsn, DB_USER, DB_PASS);
|
||
|
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||
|
} catch (PDOException $e) {
|
||
|
die('DB connection error: ' . $e->getMessage());
|
||
|
}
|
||
|
}
|
||
|
return $connection;
|
||
|
}
|
||
|
}
|
||
|
|