0

Do I get this in a base class calling an abstract class I inherit from?

class ConcreteClass extends MyAbstractClass(){
  public function __construct(){
    parent::__construct();  //or other method that returns my adapter, PDO in this case.
  }
}

Or, do I get a new adapter on each object reference created:

class ConcreteClass extends BaseClass(){  //or no class extension for that matter
  public function __construct(){
     public function __construct(DB $adapter) {  //or call DB on another class for DI via set/get
        $this->adapter = $adapter;
        $this->adapter->connect();
    }
  }
}

I would be creating a new connection to the database with each object created. Is this bad? I don't know another way to do it other than a singleton, which I do not want to use.

http request -> controller -> eventually I get my object, which makes the call to the database, new connection each time the object is created.

I have created an interface or abstract class that is of type "DB," so I can change my datasource if needs be, though I am using PDO already.

johnny
  • 19,272
  • 52
  • 157
  • 259
  • 1
    This should help http://stackoverflow.com/a/11369679/727208 .. the short answer: *first time you initialize a data mapper and then reuse the same DB connection for every other data mapper you instantiate*. – tereško Sep 04 '14 at 14:49

1 Answers1

0

You can check out how Laravel framework has solved this - see https://github.com/illuminate/database/blob/master/DatabaseManager.php. AFAIK single instance of this DatabaseManager object is stored in the Laravel ioc container.

"I would be creating a new connection to the database with each object created. Is this bad?" - it depends:

  1. When connecting to remote machine, creating new connection for each object can be time/resource consuming, especially when you have lots of objects
  2. You also may hit somekind of maximum connection limit, especially when nested classes are using separate connections (that is refcount for objects that are storing connections won't hit 0 before script execution ends).
  3. AFAIK everything transaction related can only operate on a single connection, so when you use separate connections, lets say in your persistance layer, there is no way to use transaction logic in service layer.

And IMHO, if you need to swap out datasource in PHP you are already screwed. PDO is best that PHP has to offer and it is not good at all for this - it only provides you with persistent PHP API, you still need to manually convert all vendor specific SQL.

Mikk
  • 2,209
  • 3
  • 32
  • 44
  • So is Laravel using the singleton concept? protected function createSingleConnection. – johnny Sep 02 '14 at 17:27
  • 1
    @johnny My knowledge of design pattern terminology is not as good as it should be, but essentially default behaviour for Laravel is to create connection when needed and then keep using this connection instead of creating new one (so yeah, basically singleton). Connections are kept in "string" -> "connection" map so you can connect to different databases simultaneously. Connection manager itself is accessed through the Lavarel container. – Mikk Sep 02 '14 at 17:42