tags:

views:

94

answers:

3

Hey guys 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;
    }

}

+1  A: 

You might find this article useful, it's by Eric Wurzer and it was posted in Nettuts, which is one of my other favorite websites (besides this).
Why you Should be using PHP’s PDO for Database Access

Hope this helps.

jnkrois
good article jnk but they too show the simple connection process I was using. I guess it is a good place to start.
Scarface
+1  A: 

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 :)

Nate Pinchot
appreciate it nate
Scarface
+1  A: 

My suggestion is that you implement a singleton to restrict the instantiation of PDO to one single object. It might look like this:

class Database {

    protected static $_instance;
    protected $_connection;
    protected $_dns = 'mysql:host=localhost;dbname=mydbname';
    protected $_username = 'myusername';
    protected $_password = 'mypassword';

    /**
    * Singleton pattern implementation makes "new" unavailable
    */
    protected function __construct()
    {
        $this->_connection = 
            new PDO($this->_dns, $this->_username, $this->_password);
    }

    public function getConnection()
    {
        return $this->_connection;
    }

    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
    * Singleton pattern implementation makes "clone" unavailable
    */
    protected function __clone()
    {}
}


$dbc = Database::getInstance()->getConnection();
nuqqsa
what is the benefit of switching?
Scarface
This pattern is intended to avoid multiple instances. When working with a database connection, you normally want to open that connection only once, not every time you request the PDO object. That's why the constructor and the __clone methods are not public. Its instantiation is always handled by getInstance().
nuqqsa