SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast dakine's Avatar
    Join Date
    Dec 2006
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    One file accessing two files which both use DB, error error!!

    I have one file that includes two files. These are in class structure (PHP5). I have tried numerious things like declaring the db connection in each file and also making the two classes (files) extend a db_connection class.

    The problem im getting is a fatal error stating that a method can not be re-declared. I am using adodb_lite.

    What i want to know is how do i go about using a parent class to setup the db but not have that class called twice when including the two sub classes.

    I get the error when the second class that extends db_connection is included.

    PHP Code:
    class db_connection{
        
        public 
    $db;
        private 
    $config;
            
        function 
    db_connection(){
            require(
    "/configuration.php");
            require(
    $config['ADOdb']);
            
    $this->config $config;
            
    $this->db ADONewConnection('mysql');
        }
        
        function 
    adodb_connect() {
            
    //Connect to database
            //$this->adodb_quit();
            
    $this->db->Connect($this->config['DBhost'], $this->config['DBuser'], $this->config['DBpass'], $this->config['DBname']);
            
    $ADODB_FETCH_MODE 'ADODB_FETCH_ASSOC';
        }
        
        function 
    adodb_quit(){
            
    $this->db->Close();
            
    //unset($this->db);
            
    }

    PHP Code:
    class product extends db_connection {

        public 
    $db;//Database resource
        
    var $config;

        function 
    product() {
            require(
    "/configuration.php");
        }

    PHP Code:
    class user extends db_connection{
        
        public 
    $db;
        
        function 
    user(){
        require(
    "/configuration.php");
        }

    PHP Code:
    error_reporting(E_ALL);
    session_start();
    $selected 'products';
    include_once(
    $_SERVER['DOCUMENT_ROOT'] . "/configuration.php");
    include_once(
    $config['Product']);
    include_once(
    $config['User']); //this line causes the error 
    Fatal error: Cannot redeclare class db_connection in /var/www/vhosts/domain.com/subdomains/admin/httpdocs/functions/class.db_connection.php on line 2
    Last edited by dakine; Jul 2, 2007 at 16:23.

  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)
    The error looks like both your class files are including the class file for db_connection; the solution is to change the include (or require) statement to include_once (or require_once). Can't tell for sure if that's the problem, as the code you pasted above doesn't show any include statements in the class files, but they would have to otherwise you're not going to ever load the db_connection class at all.
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    SitePoint Enthusiast dakine's Avatar
    Join Date
    Dec 2006
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I had already done that and it had chnaged the error msg, i should of posted the update.

    Now iv got a new error
    Fatal error: Call to a member function Connect() on a non-object in /var/www/vhosts/domain.com/subdomains/admin/httpdocs/functions/class.db_connection.php on line 16

    so now im just trying to find me a way of accessing the connect method without instantiating the db_connection as I have to then modify heaps of code. (the above code is just the start of the files, there are crap loads of methods)

    I can't do it statically either coz i use the $this keyword.

    Any ideas?

  4. #4
    SitePoint Enthusiast dakine's Avatar
    Join Date
    Dec 2006
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought that if a class extends another class then the child class could be used with out having to instantiate the parent first because the child inheriates everything.

  5. #5
    SitePoint Enthusiast dakine's Avatar
    Join Date
    Dec 2006
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "If you create a constructor in a child class, be aware that you are completely overriding the parent class’s constructor, and that you must call it explicitly from your new constructor if you still want the parent class to handle its share of the object initialization."

    This is the problem, connect is a method of db, db is instantiated when db_connection is called as a constructor but because im using the child classes they won't call the parent class.

    I amaze myself sometimes


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
  •