SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    Bowen Mountain, NSW
    Posts
    490
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    include() in Classes

    G'day to all,

    Just learning about classes in PHP. A couple of the methods I hold within a class call upon connecting to a database. Would it be better to use an include() function like this:
    PHP Code:
    <?php
    function registerUser($userName$userPassword) {
              
    # connect to database
              
    include('dbsignup.php');
             
    # select database
             
    mysql_select_db($this->dbName);
             
    # Code continues...
    ?>
    Many thanks in advance,


    or do I maintain using this:
    PHP Code:
    <?php
    function registerUser($userName$userPassword) {
            
    # connect to database
            
    $dbLink mysql_connect($this->dbHost$this->dbUser$this->dbPass);
            if(!
    dbLink) die("Could not connect to database. " mysql_error());
            
            
    # select database
            
    mysql_select_db($this->dbName);
            
            
    # Code continues...
    ?>
    Regards,
    BJ Duncan

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Use the second option. Although same difference technically, it's the matter of readability. If you're concerned about code duplication, employ OOP (which you say you're up to anyway) for that matter.
    Saul

  3. #3
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BJ Duncan View Post
    PHP Code:
    include('dbsignup.php'); 
    What is really bad about this from an OOP point of view is your class is tied to your application. Never code static values into classes and functions; feed these values in as arguments.

  4. #4
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,032
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    I would move the database connection to it's own class, then use instances of the database class whenever you need to connect to the database.


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
  •