SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Help with connecting to a database in a class

    Hey there. I am trying to connect to a DB in a class so I have created this class that opens a new persistent connection if one is not opened already.

    PHP Code:
    private static function connect(){
            if(!
    self::$connection){
                try{
                    
    self::$connection  = new PDO($this->type.':host='.$this->host.';dbname='.$this->name$this->user$this->password);
                }

                catch(\
    PDOException $e){
                    die();
                }
            }
                return 
    self::$connection;
        } 
    but when I try to connect to the database I end up getting a "Access to undeclared static property: Fusion\Core\db::$connection" error.

    Can someone point me in the right direction.

    Thanks!

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Would... you not be wanting to point to $this instead of self:: ?

    My understanding (and i'm not an OOP specialist, sadly) is that self:: refers to the class definition, whereas $this refers to the instantiated object of the class...

    The PDO documentation seems to indicate you should be able to establish persistance with a setting in the connection attempt...
    PHP Code:
    $dbh = new PDO('mysql:host=localhost;dbname=test'$user$pass, array(
        
    PDO::ATTR_PERSISTENT => true
    )); 
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Enthusiast Strider64's Avatar
    Join Date
    Aug 2012
    Posts
    37
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class Database {
     
        private 
    $_connection;
        
    // Store the single instance.
        
    private static $_instance;
     
        
    // Get an instance of the Database.
        // @return Database: 
        
    public static function getInstance() {
            if (!
    self::$_instance) {
                
    self::$_instance = new self();
            }
            return 
    self::$_instance;
        }
     
        
    // Constructor - Build the PDO Connection:
        
    public function __construct() {
            
    $db_options = array(
                
    PDO::ATTR_EMULATE_PREPARES => false                     // important! use actual prepared statements (default: emulate prepared statements)
                
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION           // throw exceptions on errors (default: stay silent)
                
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC      // fetch associative arrays (default: mixed arrays)
            
    );
            
    $this->_connection = new PDO('mysql:host=localhost;dbname=cms;charset=utf8''username''password'$db_options);
        }
     
        
    // Empty clone magic method to prevent duplication:
        
    private function __clone() {
     
        }
     
        
    // Get the PDO connection:    
        
    public function getConnection() {
            return 
    $this->_connection;
        }
     

    To connect to the database/table :
    PHP Code:
    $db Database::getInstance();
    $pdo $db->getConnection(); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •