www.webdeveloper.com
Results 1 to 2 of 2

Thread: Including a File Within a Class

  1. #1
    Join Date
    Sep 2007
    Posts
    10

    Question Including a File Within a Class

    I have a basic sql connect file that sets up the mysql connection that I include in the majority of my pages.

    I have recently decided to move some functions and whatnot over to a class and I'm trying to include this file within the class.

    This only works though if I include the file in EACH function which is by no means a good solution. Am I missing something? I've tried putting it outside the class, just inside, I've tried require, require_once, include, include_once

    Is this just me being stupid or is there some way I can do this, I'm trying to limit the number of places I have to edit the SQL connection so if something changes I don't have to modify 50 files.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    It could be done in the constructor, but it still might not be what you need just by including it if you depend on certain variables that it sets, as they would be local to that constructor method. You would need to copy them into class variables.

    A more modular solution might be to turn that database connection stuff into its own class, then pass an instantiation of it to your other class.

    DbConnect.php:
    PHP Code:
    class DbConnect
    {
       private 
    $connection;
       private 
    $host 'localhost';
       private 
    $user 'root';
       private 
    $pwd 'abcd1234';
       private 
    $databae 'my_database';
       public function 
    __construct()
       {
          
    $this->connection mysql_connect($this->host$this->user$this->pwd);
          if(
    $this->connection != false)
          {
             
    $result mysql_select_db($this->database$this->connection);
             if(
    $result == false)
             {
                throw new 
    Exception("Database select failed");
             }
          }
          else
          {
             throw new 
    Exception ('Database connect failed');
          }
       }
       public function 
    connx()
       {
          return 
    $this->connection;
       }

    DoSomeDbStuff.php:
    PHP Code:
    class DoSomeDbStuff
    {
       private 
    $db;
       public function 
    __construct(DbConnect $db)  // Type-hinting
       
    {
          
    $this->db $db;
       }
       public function 
    foo()
       {
          
    $result mysql_query('SELECT * FROM anything'$this->db->connx());
          return 
    $result;
       }

    test.php:
    PHP Code:
    require_once 'DbConnect.php';
    require_once 
    'DoSomeDbStuff.php';
    try
    {
       
    $db = new DbConnect();
    }
    catch(
    Exception $e)
    {
       die(
    'Oh crap!');
    }
    $test = new DoSomeDbStuff($db);
    $queryResult $test->foo(); 
    The above is crude and not necessarily how I'd do it, just a quick-and-dirty example to hopefully give you some food for thought.
    "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