Php PDO database singleton. Which option to choose?

As `kosher` to implement access to the database, so that each time you appeal not to create a new connection? I see two options. First realize this:
1. file Database.php
<?php

class Database extends PDO
{

 function __construct()
{
 parent::__construct('mysql:host=localhost;dbname=mydbname', username, password);
}
}</property-->
br><br> 2. file conditionally index.php br><pre><code><?php require_once Database.php; class MyClas { function __construct() { $this--->db = new Database(); } function run() { $stmt = $this->db->prepare(....); } function callHook() { $stmt = $this->db->prepare(....); } }</code></pre><br><br> the second is to implement the Database class as a singleton. br><br> Tell me, who long ago wrote in php. Do when using the first option will not constantly be installing a new connection. And if you implement using singleton as `kosher` to do this using PDO. Well, or is there another reliable and easy option. Thank you.
September 19th 19 at 00:27
2 answers
September 19th 19 at 00:29
Solution
Both options are disgusting.
To properly establish a connection in one place and then transfer it to where it is needed.
i.e. to use a singleton? - glen_Boyer36 commented on September 19th 19 at 00:32
no. The singleton anti-pattern. - Missouri.Hoeg commented on September 19th 19 at 00:35
: throw me a link to an example of the correct from your point of view implementation. Or maybe a couple of lines of sample code here. Thank you. - glen_Boyer36 commented on September 19th 19 at 00:38
: www.cyberforum.ru/php-oop/thread1359894.html - Missouri.Hoeg commented on September 19th 19 at 00:41
: I.e., in the bootstrap I create connection $db = new PDO(...). And then when you create a class vezede to convey this: Auth::login($this->db, $name, $pass); b = new SomeClass($this->db); ? - glen_Boyer36 commented on September 19th 19 at 00:44
It is not necessary to use static methods. And pass better in the designer. - Missouri.Hoeg commented on September 19th 19 at 00:47
: And I say, if you have a class did not keep, do not need to create an instance of it and what do I do if the Auth class only checks the user's validity, well, there is more trivia in the future - glen_Boyer36 commented on September 19th 19 at 00:50
So you have a class stores $dbh. - Missouri.Hoeg commented on September 19th 19 at 00:53
: How to pass in a constructor. don't you transmitted a = new ClassA($param1, $param2, .., $paramN)? And I realized that stores the authorization class of the user? - glen_Boyer36 commented on September 19th 19 at 00:56
define what you're talking about. you secalis in a bunch of horses, people, applied classes ,authorization, access to DB... Where are the General authorization come from? - Seth commented on September 19th 19 at 00:59
I agree with FanatPHP until forget about authorization. Do part, but correctly. - Missouri.Hoeg commented on September 19th 19 at 01:02
OK, OK, you are right, authorization is a separate issue. I do not understand how to pass it in the constructor - glen_Boyer36 commented on September 19th 19 at 01:05
...how to pass in the constructor, if the class is static? To do he has a method init which cause before use. - Seth commented on September 19th 19 at 01:08
: I agree. But what does it mean to pass in the constructor? - glen_Boyer36 commented on September 19th 19 at 01:11
I wrote you an example - Seth commented on September 19th 19 at 01:14
: that is said about a static class apparently, there is to transfer in the designer - glen_Boyer36 commented on September 19th 19 at 01:17
: : it's been said that it is necessary to remove from the class are all static. - Missouri.Hoeg commented on September 19th 19 at 01:20
: I created a separate question on the authorization, if you look, I would be very grateful. - glen_Boyer36 commented on September 19th 19 at 01:23
September 19th 19 at 00:31
According to the first embodiment:
1. Why do we need a Database class?
2. You will have only one class in the entire application? or more? And how will new connections after new myclass and myclass2?

For the second one.
Highbrow don't like singleton. Something in the testing prevents. Plus religious intolerance. So use the static singleton only if you have code organized in a classic procedurecolumnout.

If you're a kosher class hierarchy, as seen in another answer, pass the connection into the class, not create it every time again.
function __construct($db)
{
 $this->db = $db;
 }
Ie buttrape I have to inicializirati connection $this->db = new PDO(); And then pass in the class: a = new ClassA($this->db); bb= new ClassBB($this->db); but if I have inside these classes there are still classes that use the connection you need and they give in to their subclasses, and so on? - glen_Boyer36 commented on September 19th 19 at 00:34
yeah - Missouri.Hoeg commented on September 19th 19 at 00:37
Singletons do not interfere with the testing, if it is to push through the class constructor or via setter, not everywhere, to call on the line. - glen_Boyer36 commented on September 19th 19 at 00:40
: logical. But in this case it turns out not really need, I think
. - Missouri.Hoeg commented on September 19th 19 at 00:43
: in this approach, it is extremely beneficial to look in comparison with the same container that is used in symfony 2. Because in most cases, we need do only copy. We work with a concrete class, and not a string key.
function __construct($container)
{
 $con = $container->get('connection');
}

function __construct(Connection $con=NULL)
{
 if ($con == NULL) {
 $con = Connection::getInstance();
}
}

In the first case, it is almost global variables, with all the consequences. - glen_Boyer36 commented on September 19th 19 at 00:46
: damn! I always offered to do so, and kicked me. And a new connection is created by inheriting the base class connection extends BaseConnection class SlaveConnection. Right? - Missouri.Hoeg commented on September 19th 19 at 00:49
About the new compound do not understand. What is inheritance? If you have many connections, then there is no suitable solution with a singleton.
But in most cases, when writing websites, you need only one connection for all application. And here it is, singleton fits very well. - glen_Boyer36 commented on September 19th 19 at 00:52
: But why not multiple singletons? - Missouri.Hoeg commented on September 19th 19 at 00:55
: a few singleton if they are of different types. For the essence of the pattern that this object needs only one copy always in the application.
If you need several, then you need to design taking into account the various connections that are needed at the same time.
I honestly don't understand why you two connection app what is created through inheritance. - glen_Boyer36 commented on September 19th 19 at 00:58
: well, of course! Two connexin - or master and slave, or mysql and Sphinx. Inheritance only need to distinguish between them. - Seth commented on September 19th 19 at 01:01
>mysql and Sphinx.
: This is a totally different connection. No matter what you might be to use the same driver somewhere at a low level, from the point of view of working with them, they are two completely different entities: DBConnection, SphinxConnection. As perform very different functions.

> master and slave
Well, at the application level, it is actually strange to realize. Should think about that or the cluster, or a separate service that will implement the connection.
But if you need to implement in your application, then implement the class that will return the already prepared copy of the required connections:
class Connection {
 static function getInstance() {
 // do you hear the catch? We don't need Connection::getInstace()
 return ConnectController::getInstance()->getConnection();
}
}
- Missouri.Hoeg commented on September 19th 19 at 01:04
: well, what did I suggest? - glen_Boyer36 commented on September 19th 19 at 01:07
: As far as I understood you,you suggest the following:
class BaseConnection {
 const connection_string = "pgsql:host=localhost;port=5432";
 public static function getInstace() {
 //... use static::connection_string
}
}

class SlaveConnection extends BaseConnection {
 const connection_string = "pgsql:host=192.168.1.2;port=5432";
}

What a completely different "smells" :) - Seth commented on September 19th 19 at 01:10

Find more questions by tags PHPPatterns of designingPHP PDO