note 93178 modified in book.pdo by danbrown

From: Date: Thu, 27 Aug 2009 16:34:22 +0000
Subject: note 93178 modified in book.pdo by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159935@lists.php.net to get a copy of this message
I decided to create a singleton wrapper for PDO that ensures only one instance is ever used. It uses PHP 5.3.0's __callStatic functionality to pass on statically called methods to PDO. This means you can just call it statically from anywhere without having to initiate or define the object. Usage examples: <?php DB::exec("DELETE FROM Blah"); foreach( DB::query("SELECT * FROM Blah") as $row){ print_r($row); } ?> Code: <?php class DB { private static $objInstance; /* * Class Constructor - Create a new database connection if one doesn't exist * Set to private so no-one can create a new instance via ' = new DB();' */ private function __construct() {} /* * Like the constructor, we make __clone private so nobody can clone the instance */ private function __clone() {} /* * Returns DB instance or create initial connection * @param * @return $objInstance; */ public static function getInstance( ) { if(!self::$objInstance){ self::$objInstance = new PDO(DB_DSN, DB_USER, DB_PASS); self::$objInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } return self::$objInstance; } # end method /* * Passes on any static calls to this class onto the singleton PDO instance * @param $chrMethod, $arrArguments * @return $mix */ final public static function __callStatic( $chrMethod, $arrArguments ) { $objInstance = self::getInstance(); return call_user_func_array(array($objInstance, $chrMethod), $arrArguments); } # end method } ?> --was-- I decided to create a singleton wrapper for PDO that ensures only one instance is ever used. It uses PHP 5.3.0's __callStatic functionality to pass on statically called methods to PDO. This means you can just call it statically from anywhere without having to initiate or define the object. Usage examples: DB::exec("DELETE FROM Blah"); foreach( DB::query("SELECT * FROM Blah") as $row){ print_r($row); } <? class DB { private static $objInstance; /* * Class Constructor - Create a new database connection if one doesn't exist * Set to private so no-one can create a new instance via ' = new DB();' */ private function __construct() {} /* * Like the constructor, we make __clone private so nobody can clone the instance */ private function __clone() {} /* * Returns DB instance or create initial connection * @param * @return $objInstance; */ public static function getInstance( ) { if(!self::$objInstance){ self::$objInstance = new PDO(DB_DSN, DB_USER, DB_PASS); self::$objInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } return self::$objInstance; } # end method /* * Passes on any static calls to this class onto the singleton PDO instance * @param $chrMethod, $arrArguments * @return $mix */ final public static function __callStatic( $chrMethod, $arrArguments ) { $objInstance = self::getInstance(); return call_user_func_array(array($objInstance, $chrMethod), $arrArguments); } # end method } ?> http://php.net/manual/en/book.pdo.php

« previous php.notes (#159935) next »