Jump to content


Cannot redeclare class


12 replies to this topic

#1 mikem

    Young Padawan

  • Members
  • Pip
  • 11 posts
  • Gender:Male
  • Location:Fort Bragg, NC

Posted 15 September 2007 - 12:14 AM

I may be missing something here but Im having a bit of trouble with this code here. Can someone please lend me a hand? In advance, thank you so much...

Here is the codes and error below:
Error:

Quote

Fatal error: Cannot redeclare class cdb in F:\SERVER\htdocs\core\_database.php on line 11

Here are the short codes:
class cdb {
      // Construction
  
      function cdb() {
  
          // When initiated, it immidietly runs the connect function
  
          $this->connect();
  
      }
  
      // Connects to database server and to the database
  
      function connect() {
  
          // Connect to database server
  
          mysql_connect(SQL_HOST,SQL_USER,SQL_PASS) or die("Couldn't connect to database!");
  
          // Connect to database
  
          mysql_select_db(SQL_DB) or die("Coudln't find table!");
      }
      // Closes connection
      function close() {
          // disconnects
          mysql_close();
      }
      // Makes a query and returns the result
  
      function query($str) {
  
          // query the mysql statement
  
          $result = mysql_query($str);    
  
          // returns the result
  
          return $result;
  
      }    
  
      // Function to gather all the configrations located in the database.
  
      function get_config() {
  
          // sql statement
  
          $sql = "SELECT * FROM ".SUFFIX."config";
  
          // gather result
  
          $result = $this->query($sql);
  
          // if there are results go on
  
          if(mysql_num_rows($result)) {
  
              while($row = mysql_fetch_assoc($result)) {
  
                  // if a constant is not defined
  
                  if(!defined($row['name'])) {
  
                      // define a new constant with the database name + value
  
                      define($row['name'], $row['value']);
  
                  }
  
              }
  
          }
  
      }
  
  }
  
      


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 15 September 2007 - 12:33 PM

Well, thanks for providing the class, but the real question is, what is in F:\SERVER\htdocs\core\_database.php on and before line 11?

#3 mikem

    Young Padawan

  • Members
  • Pip
  • 11 posts
  • Gender:Male
  • Location:Fort Bragg, NC

Posted 15 September 2007 - 02:03 PM

View Postrc69, on Sep 15 2007, 05:33 PM, said:

Well, thanks for providing the class, but the real question is, what is in F:\SERVER\htdocs\core\_database.php on and before line 11?
Well as you can see above that, this is a snippet taken from a tutorial inwhich you guys host. I also cannot get a hold of the guy who write it to solve this issue. Im sure others may have had this same problem as well. So, here is the link to the tutorial and the answer to your question.
/core/_database.php

../tutorials/607/creating_a_simple_discussion_board/page3/
Also, thanks for replying back too :o

Edited by mikem, 15 September 2007 - 02:04 PM.


#4 nitr0x

    Young Padawan

  • Members
  • Pip
  • 201 posts

Posted 15 September 2007 - 05:27 PM

What PHP version do you have? It's something to do with the:

function cdb() {

PHP thinking you're declaring the class again.

#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 15 September 2007 - 05:57 PM

You have to keep in mind, just because we host it, doesn't mean i know about it.

Quote

function cdb() {

PHP thinking you're declaring the class again.
Now, while that may appear to be the problem, i don't see how it's possible. To start, it's a constructor, which is perfectly valid php (ref:OOP).
Second, it's inside the class. PHP typically complains about redeclarations of functions/classes when the file defining it is included more than once (typical beginner mistake). So the error should be on line 8 (provided he copied/pasted the tutorial exactly).

Unfortuantely, i don't have time to run through the entire 15 page tutorial to figure out where this could possibly be occuring. What i can say is, if it is the same line 11 from the tutorial, then you may be up the creek without a paddle... Then again, i could be wrong and PHP actually complains about redeclaring classes at the first instance of a function definition... again, don't have time to test this theory.

#6 Balor

    PHP Nerd

  • Members
  • Pip
  • 63 posts
  • Gender:Male
  • Location:Germany->Frankfurt
  • Interests:My beautyful girl, my son and webcoding. I'm also very interested in art but I'm not really good at art... it's sad but true ^^

Posted 16 September 2007 - 12:51 AM

Hey,


thanks for trying my tutorial. Already got some guys having problem with the tut, but mostly because of smarty. That's the first time I see someone having problem with the simple database class. However... you copy & pasted it right? If you copy & pasted the class correctly, the error can't be in the class itself, as it's definantly working and valid. The error has to be somewhere else.

In index.php, do you execute the db class more than once?

Is this problem already solved? If not, please send me a pm... I will help you on this tutorial.

#7 mikem

    Young Padawan

  • Members
  • Pip
  • 11 posts
  • Gender:Male
  • Location:Fort Bragg, NC

Posted 16 September 2007 - 12:51 AM

Thank you for your help and input rc96 Very helpful inwhich there can only be so much said. Anyways, So do you belive in has something to do with what version of php im running?

#8 Balor

    PHP Nerd

  • Members
  • Pip
  • 63 posts
  • Gender:Male
  • Location:Germany->Frankfurt
  • Interests:My beautyful girl, my son and webcoding. I'm also very interested in art but I'm not really good at art... it's sad but true ^^

Posted 16 September 2007 - 12:59 AM

I personally don't think it's the php version... there is nothing special in here that requires php5 or a more advanced php version.

#9 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 16 September 2007 - 11:37 AM

Try adding this to the top of that class file, so you can debug a bit.

<?php
// If this file has been included before
if(defined('CDB')){
// Let's try to get an array and see where this might be included
var_dump(get_included_files());
var_dump(get_required_files());

// Now let's dump a debugging backtrace and maybe go from there
var_dump(debug_backtrace());

// And end execution
exit;
}

// Define that this file has been called
define('CDB', true);

// The rest of the file and class declaration...
?>

Go ahead and add that, and if you are confused by the output and can't follow it, post it here and we can help you out, and maybe find out where the file is being included a second time.

Also, what you could always do, is always just issue a return if the CDB constant is defined, so it will stop the execution of that file and continue on with whatever else.

#10 mikem

    Young Padawan

  • Members
  • Pip
  • 11 posts
  • Gender:Male
  • Location:Fort Bragg, NC

Posted 16 September 2007 - 04:37 PM

will do.

#11 mikem

    Young Padawan

  • Members
  • Pip
  • 11 posts
  • Gender:Male
  • Location:Fort Bragg, NC

Posted 16 September 2007 - 04:40 PM

View PostBalor, on Sep 16 2007, 05:59 AM, said:

I personally don't think it's the php version... there is nothing special in here that requires php5 or a more advanced php version.

Well Balor, no affence, but this is your tutorial. You also show in the tutorial the demo of what it should look like once you are finished. I saw the demo and it looks like everything works fine. Think you can backtrack the tutorials a bit to make sure the codes are correctly matching with what you have on the demo? http://www.uwserver.de :D :D

Edited by mikem, 16 September 2007 - 04:41 PM.


#12 Balor

    PHP Nerd

  • Members
  • Pip
  • 63 posts
  • Gender:Male
  • Location:Germany-&gt;Frankfurt
  • Interests:My beautyful girl, my son and webcoding. I'm also very interested in art but I'm not really good at art... it's sad but true ^^

Posted 17 September 2007 - 01:23 AM

it's exactly the same and it worked on php4 + php5 environments...

#13 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 17 September 2007 - 04:37 PM

There are no specific changes in PHP 4 to 5 that would render problems such as this. Especially this kind of error.
This error usually only can occur when you are including the same file multiple times.

What is your output from the code I gave you?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users