www.webdeveloper.com
Results 1 to 2 of 2

Thread: Best practices for db access in PHP5

  1. #1
    Join Date
    Feb 2004
    Posts
    36

    Best practices for db access in PHP5

    Hi,

    I really wanted to get an idea of the way people handle their database connections and queries in PHP5.

    I'm not really sure of the best approach.
    What I'm doing at the moment is defining a singleton class to hold a PDO connection object, then defining a class with static methods for querying:

    PHP Code:
    // Singleton class to hold db connection
    class db
    {
        private static 
    $_instance;
        private static 
    $_connection;
        private static 
    $_user='myUser';
        private static 
    $_pass='myPass';
        private static 
    $_host='myHost';
        private static 
    $_dbname='myDB';

        protected final function 
    __construct()
        {
            try
            {
                
    self::$_connection = new PDO('mysql:host='.db::$_host.';dbname='.db::$_dbnamedb::$_userdb::$_pass, array(PDO::ATTR_PERSISTENT => true));    
            }
            catch (
    PDOException $e)
            {
                print 
    "Error!: " $e->getMessage() . "<br/>";
                die();
            }
        }

        public static function 
    getInstance()
        {
            if (!
    self::$_instance instanceof self)
            { 
              
    self::$_instance = new self;
            }
            return 
    self::$_instance;
        }

        public function 
    con()
        {
            return 
    self::$_connection;
        }

    PHP Code:
    // Data Access Layer
    class dal
    {
        private static function 
    getResults($query)
        {
            return 
    db::getInstance()->con()->query($query,PDO::FETCH_ASSOC);
        }
        private static function 
    getResult($query)
        {
            return 
    db::getInstance()->con()->query($query,PDO::FETCH_ASSOC)->fetch();
        }
        
        public static function 
    getProducts()
        {
            
    $sql 'select * from products';
            return 
    self::getResults($sql);
        }


    What do you think of this approach? Is there a better way?

    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,248
    I prefer to pass the database object as a constructor parameter to those classes that need it.
    PHP Code:
    class Model
    {
       private 
    $db;

       public function 
    __construct(PDO $db)
       {
          
    $this->db $db;
       }
    }

    $db = new PDO($dsn);
    $model = new Model($dsn);
    $anotherModel = new Model($dsn); 
    This still gives you only one PDO instance (remember that by default PHP5 passes object references to functions), while keeping the class interfaces clear and easy to debug; and it also allows you to have separate PDO instances should you ever have a reason to need more than one.

    Of course, my real preference is to use a framework (such as CodeIgniter) where stuff like this is handled "behind the scenes" and I don't have to worry about it.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles