user961627
user961627

Reputation: 12747

pdo connection position in php class

I'm using PDO now and I have a number of classes. Every time I use a class, I don't always use its database-related functions. Sometimes I keep using a class until at the end I might do some work with the database, like save this object to DB.

So I'm currently doing something like this:

class Something
{
   protected $pdo;

     function connect()
     {
        $this->pdo = new PDO( "mysql:host=".zConfig::read('hostname').";dbname=".zConfig::read('database'), zConfig::read('username'), zConfig::read('password'));
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     }

     /* lots of functions doing lots of non-DB things */

     function saveToDB()
     {    $this->connect();
          $this->pdo->prepare("Some SQL that saves some stuff");
          // now do some DB-related pdo work
     }

}

My question is - is this reasonable? Is this the way a lot of you code?

As I see it, there are 3 options:

  1. The way I'm doing it - connecting to the database only in those functions that are database related. But these means each time I run a DB-related function.
  2. Open a connection in the constructor, in this case I only open it once, but also end up opening it even when I don't need it, so I feel like sometimes I'll be creating connections for nothing.
  3. On the other hand, I could use a singleton database class like this:

    PDO Connections - max connections

I do have up to three related objects on a page, and sometimes I do and sometimes I don't need to connect to the DB more than once on a page. which design is safer?

Upvotes: 0

Views: 132

Answers (2)

samayo
samayo

Reputation: 16495

You should use DI, in your class to instantiate a connection, when your object is defined ex:

class foo{
    public $conn;

    function __construct($conn){
    $this->conn = $conn;
    }

    function doSomething(){
    }
}

Now, if you find yourself, not wanting to instanciate a connection, whenever you are on a page/work that does not need database connection, and significantly slows your page, while trying to connect, use the PDO attribute

ATT_PERSISTENT property like:

$conn = new PDO('mysql:host=localhost;dbname=xxx', 'xxx', 'xxx', 
                array( PDO::ATTR_PERSISTENT => true )
        );

$object = new foo($conn);

Simply, once, you open a page, and connection is establish the ATTR_PERSISTENT method will store that connection, pretty much like sessions work, and will keep feeding your page, and helping you from creating a new connection to your db, every time you refresh a page, or go on to another page. Try, it.. and you'll see how faster your pages will load.

Upvotes: 2

sectus
sectus

Reputation: 15464

You can just use lazy way.

 protected $pdo;

 function connect()
 {
 if ($this->pdo === null)
    {
    $this->pdo = new PDO( "mysql:host=".zConfig::read('hostname').";dbname=".zConfig::read('database'), zConfig::read('username'), zConfig::read('password'));
    $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
 }

Check this question.

Upvotes: 2

Related Questions