PHP Classes

PHP PDO Connection Factory: Create PDO objects to connect to a database

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 515 All time: 5,735 This week: 455Up
Version License PHP version Categories
connection-factory 1.5GNU General Publi...5.1PHP 5, Databases


This class can create PDO objects to connect to a database.

It can take an array of parameters to establish a connection with a database using PDO.

The parameters can define values like the database driver name, database host, user, password and database name.

The class may also change individual connection parameters after the class object is created.

In the end it returns an object of the PDO class ready to be used by the application.

Picture of Raul
  Performance   Level  
Name: Raul is available for providing paid consulting. Contact Raul .
Classes: 7 packages by
Country: Brazil Brazil
Age: 33
All time rank: 61143 in Brazil Brazil
Week rank: 307 Up16 in Brazil Brazil Up


use Core\lib\db\ConnectionFactory;
 * The test file is in Core folder
 * Place the class on Core/lib/db
 * directory to use autoloading
require_once 'Core/lib/db/ConnectionFactory.php';

$arguments = [
'driver' => 'mysql',
'host' => 'localhost',
'user' => 'root',
'password' => '',
'database' => 'dbname',

/*PHP 5.4+ style - just connect*/
$pdo = (new ConnectionFactory($arguments))->getLink();

/*PHP 5.3- style - just connect*/
$connectionFactory = new ConnectionFactory($arguments);
$pdo = $connectionFactory->getLink();

 * You can use the method getLink or the alias getConnection() as well
$pdo = $connectionFactory->getConnection();

 * You can set any attribute or driver option
// Attributes (will add on setAttribute PDO's method
$connectionFactory->addAttribute(\PDO::ATTR_CASE, \PDO::CASE_LOWER);
$connectionFactory->addAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ);
// Driver's option (will add on the fourth parameter on PDO constructor)
$connectionFactory->addDriverOption(\PDO::ATTR_PERSISTENT, true);
$connectionFactory->addDriverOption(\PDO::MYSQL_ATTR_MAX_BUFFER_SIZE, 1024 * 1024 * 10);

$pdo = $connectionFactory->getLink();

 * Now you can use all the PDO's methods!

  Files folder image Files  
File Role Description
Files folder imageCore (1 directory)
Accessible without login Plain text file test.php Example Testing file

  Files folder image Files  /  Core  
File Role Description
Files folder imagelib (1 directory)

  Files folder image Files  /  Core  /  lib  
File Role Description
Files folder imagedb (1 file)

  Files folder image Files  /  Core  /  lib  /  db  
File Role Description
  Plain text file ConnectionFactory Class Class file

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:5,735
This week:455Up