Problems with Database Singleton Class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,
 
I am trying to create a singleton database class that wraps up the
creation of the PDO class.  However, when I call the getInstance()
method and run it through the gettype() method (for testing purposes) it
returns 'object' but when I try to create a statement object I recieve a
Fatal error: Call to undefined method WebDBConn::setAttribute()...
 
Here is my class:
 
/**
 * Class WebDBConn (Singleton Pattern)
 * This class ensures only one instance of a database
 * connection is used throughout application
 */
class WebDBConn {
 
  /**
   * DB Instance
   * @var object
   * @access private
   */
  private static $instance = NULL;
 
  /**
   * WebDBConn Constructor
   * @return void
   * @access private
   */
  private function __construct() {
 
    self::$instance = new PDO($db, $user, $pwd);
    self::$instance->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
  }
 
  /**
   * Return DB instance or facilitate intitial connection
   * @return object
   * @access public
   */
  public static function getInstance() {
 
    if (!self::$instance) {
      self::$instance = new WebDBConn;
    }
    return self::$instance;
  }
}
 
Here is the code within my application:
 
$db = WebDBConn::getInstance();
$db->prepare($sql);  (Fatal Error occurs right here -
WebDBConn::prepare() undefined method)
 
Shouldn't $db be an instance of PDO???
 
Any help would be greatly appreciated.
 
Ken Vandegrift
 
 
 
 
 

[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux