singleton factory connection pdo

Posted by Scarface on Stack Overflow See other posts from Stack Overflow or by Scarface
Published on 2010-05-16T18:08:03Z Indexed on 2010/05/16 18:10 UTC
Read the original article Hit count: 351

Filed under:
|
|
|

Hey guys I am having a lot of trouble trying to understand this and I was just wondering if someone could help me with some questions. I found some code that is supposed to create a connection with pdo. The problem I was having was having my connection defined within functions. Someone suggested globals but then pointed to a 'better' solution http://stackoverflow.com/questions/130878/global-or-singleton-for-database-connection. My questions with this code are. PS I cannot format this code on this page so see the link if you cannot read

  1. What is the point of the connection factory? What goes inside new ConnectionFactory(...)

  2. When the connection is defined $db = new PDO(...); why is there no try or catch (I use those for error handling)? Does this then mean I have to use try and catch for every subsequent query?

class ConnectionFactory { private static $factory; public static function getFactory() { if (!self::$factory) self::$factory = new ConnectionFactory(...); return self::$factory; }

private $db;

public function getConnection() {
    if (!$db)
        $db = new PDO(...);
    return $db;
}

}

function getSomething() { $conn = ConnectionFactory::getFactory()->getConnection(); . . . }

© Stack Overflow or respective owner

Related posts about php

Related posts about pdo