Answer the question
In order to leave comments, you need to log in
What pattern to implement to work with the database?
I plan to move away from the constructions of the form
<?php
namespace Common;
final class Connection
{
protected $link;
private static $instance = NULL;
private function __construct() {}
protected function __clone() {}
public static function getInstance() {
if(is_null(self::$instance))
{
self::$instance = new self();
}
return self::$instance;
}
public function init() {
if ( is_null($this->link) ) {
try {
$attr = array(
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => TRUE,
);
$this->link = new \PDO("mysql:host=localhost;dbname=admin;charset=utf8", "admin", "admin", $attr);
} catch (\PDOException $e) {
echo "Ошибка БД";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
}
}
/**
* @return \PDO;
*/
public function link() {
return $this->link;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question