D
D
dev4002016-05-09 12:54:13
PHP
dev400, 2016-05-09 12:54:13

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

2 answer(s)
M
Mikhail Osher, 2016-05-09
@dev400

factory.

J
jaxel, 2016-05-10
@jaxel

Why shit code another bike? Do you really believe that you can do better than existing solutions? Take doctrine for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question