Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any suggestions to improve my PDO connection class?

Tags:

php

pdo

I am pretty new to pdo, so I basically just put together a simple connection class using information out of the introductory book I was reading. But is this connection efficient? If anyone has any informative suggestions, I would really appreciate it.

class PDOConnectionFactory{

    public $con = null;
    // swich database?
    public $dbType  = "mysql";
    
    // connection parameters

    public $host    = "localhost";
    public $user    = "user";
    public $senha   = "password";
    public $db  = "database";
    

    public $persistent = false;
    
    // new PDOConnectionFactory( true ) <--- persistent connection
    // new PDOConnectionFactory()       <--- no persistent connection
    public function PDOConnectionFactory( $persistent=false ){
        // it verifies the persistence of the connection
        if( $persistent != false){ $this->persistent = true; }
    }
    
    public function getConnection(){
            try{
                $this->con = new PDO($this->dbType.":host=".$this->host.";dbname=".$this->db, $this->user, $this->senha, 
                array( PDO::ATTR_PERSISTENT => $this->persistent ) );
                // carried through successfully, it returns connected
                return $this->con;
            // in case that an error occurs, it returns the error;
            }catch ( PDOException $ex ){  echo "We are currently experiencing technical difficulties.  ".$ex->getMessage(); }

    }
    
    // close connection
    public function Close(){
        if( $this->con != null )
            $this->con = null;
    }
}
like image 218
Scarface Avatar asked Jun 09 '10 21:06

Scarface


People also ask

Does PDO close connection?

The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted—you do this by assigning null to the variable that holds the object.

What is a PDO connection?

To standardize and streamline development practices, PHP introduced PHP Data Objects (PDO) in PHP 5.1. These objects are used to setup PDO database connections. PDO is a database access layer which provides a fast and consistent interface for accessing and managing databases in PHP applications.


1 Answers

When implementing a "Factory" usually it is so that other classes, methods, etc using it don't have to know or care about the connections, usernames, passwords, etc.

I would do it something more like:

static class PDOConnectionFactory {
    // database
    private $dbType = "mysql";

    // connection parameters
    private $host = "localhost";
    private $user = "user";
    private $senha = "password";
    private $db = "database";

    // new CreateNewConnection( true ) <--- persistent connection
    // new CreateNewConnection()       <--- no persistent connection
    public function CreateNewConnection($persistent = false) {
        try {
            $con = new PDO($dbType . ":host=" . $host . ";dbname=" . $db, $user, $senha, array(PDO::ATTR_PERSISTENT => $persistent));
            // carried through successfully, it returns connected
            return $con;
        }
        catch (PDOException $ex) {
            // in case that an error occurs, it returns the error;
            echo "We are currently experiencing technical difficulties. We have a bunch of monkies working really hard to fix the problem. Check back soon: " . $ex->getMessage();
        }
    }
}

Then you use the connection returned by CreateNewConnection() in whatever way you need.

I didn't check if the above code compiles, there could be a few typos/issues, but you get the idea. Now you need to take it a step further and implement something like the repository pattern :)

like image 93
Nate Pinchot Avatar answered Sep 27 '22 21:09

Nate Pinchot