SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OOD - Database Connections

    Hi All,

    I have an implementation question regarding OOD in PHP. Say I have a class called the UserSystem class which is essentially a class that acts as an interface to the database. This class is used to manipulate all user data (eg. add users, edit users, delete users, get users).

    Thus is contains a series of functions to all these tasks. Now my question is whether each individual function should have a database connection statement. In other words, should I open the the database connection in the function, run my query, and then close the database connection. Or should I be declaring some sort of persistence database connection from a file that uses this class and assume that a connection is open when I call these functions.

    I guess my question is more of what is the general consensus on this matter.? Like either way would work, but I would like to know which way is better.

    Thanks

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by FCC View Post
    In other words, should I open the the database connection in the function, run my query, and then close the database connection.
    That's a resounding "NO!" You'll cause yourself endless headaches and slow your script down considerable (there was a thread a couple weeks ago from someone who did just this and was having no end of issues, all of which were resolved the moment he switched to a single persistent connection).

    What I do in such cases is use the class's constructor to open the database connection on instantiation and then store that in a (private) class variable. In other cases where multiple classes need access to a database connection, I open the connection outside any class and pass it to the constructor as I instantiate each object.

    As examples:
    PHP Code:
    //open connection in class constructor
    class foo
    {
        private 
    $db;
        public function 
    __construct()
        {
            
    $this->db mysql_connect(/*...*/);
        }
    }

    //pass database connection into object
    class bar
    {
        private 
    $db;
        public function 
    __construct($conn)
        {
            
    $this->db $conn;
        }
    }

    //usage of class foo
    $foo = new foo//database connection opened automatically

    //usage of class bar
    $db mysql_connect(/*...*/);
    $bar = new bar($db); //pass in open database connection 
    Note that I used PHP5 syntax here - if you're using PHP4, the constructor would be named the same as the class name instead of __construct (i.e. foo's constructor would be 'public function foo()' instead of 'public function __construct()').
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would I handle using functions of a static class?

  4. #4
    SitePoint Enthusiast Chousho's Avatar
    Join Date
    Jun 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by FCC View Post
    How would I handle using functions of a static class?
    http://www.phpbuilder.com/manual/en/...op5.static.php

    I was wondering. Would it be better to do this when working with a database connection in other classes:

    Instantiate the db class

    In the other classes, rather than pass the $db (or whatever name) var to the constructor, to check if there is an object already matching the Database class, and set the member in the other classes that way.

    As I'm not too familiar with the syntax of doing this, it would basically be
    PHP Code:
    class Blah {
    private 
    $dbc;

    public function 
    __constructor() {

    // Not sure on the syntax of this part
    if ( exists(Database::instance) ) {
    $this->dbc $db_instance;
        }
      }


  5. #5
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh opps. I was actually referring to using database connections inside of a static class. Because I can't make instances of the static class, so I won't be able to pass in any database objects.

    What is the consensus on handling database connections inside of a static class?

  6. #6
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone know?

  7. #7
    SitePoint Enthusiast Chousho's Avatar
    Join Date
    Jun 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, would it be possible to just use the global $var that works with the database?

    I don't do this; I pass the db connection through the constructor (or method, depending on what I'm working with), since furtherdown I might need to use the classes with some other project and may not have the same name for the db var.

  8. #8
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by FCC View Post
    Oh opps. I was actually referring to using database connections inside of a static class. Because I can't make instances of the static class, so I won't be able to pass in any database objects.
    A static method is just a normal function in disguise. You access variables in the same way. Eg. you can use a global variable of some sort, or you can pass the variable in as a parameter.


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
  •