Jump to content


Corporate Sponsors


Latest News: (loading..)

- - - - -

"Cannot redeclare ..."


3 replies to this topic

#1 germ

  • Community Member
  • 13,586 posts
  • Real Name:Jim
  • Gender:Male
  • Location:USA (GMT-6)

Posted 19 June 2011, 18:44

So you're developing your site and you make a few changes and suddenly this error crops up:

Fatal error: Cannot redeclare balh-blah-blah (previously declared in /www/path/to/your/file/filename.php on line xxx)

The keyword in the error is "redeclare".

This means the site is parsing the file more than once. In PHP certain items can only be "defined" once (functions and classes for example). If the file they are in is parsed by the PHP processor more than once you'll probably end up with a "Cannot redeclare ...." error.

Here's how to find out where the file is getting parsed.

To show how this works and how to use it I purposely introduced a "Cannot redeclare ...." error on my WAMP server.

Here's my error:

Quote

Fatal error: Cannot redeclare class oscTemplate in F:\wamp\www\231\includes\classes\osc_template.php on line 33
So I look at the error and it tells me what file the error is IN:

Quote

Fatal error: Cannot redeclare class oscTemplate in F:\wamp\www\231\includes\classes\osc_template.php on line 33

So I go to \includes\classes\osc_template.php and I look at the code.

Every osC file has the GNU General Public License declaration at the top of the file, it looks about like this:

/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/
So I take the code below and place it just AFTER that code but BEFORE all the other code in the file:

//* * * * start of backtrace debug code * * *
  $dbt = debug_backtrace();
  echo "<div><br>= = = = = = = = Backtrace = = = = = = = =<br>\n";
  for ( $d_b_t = 0 ; $d_b_t < count($dbt) ; $d_b_t++ ) {
    if ( $d_b_t == 0 ) 
      echo basename( __FILE__ ) . ' is referenced in ';
    else {
      echo $dbt[$d_b_t - 1]['file'] . ' is referenced in ';
    }
    if ( isset( $dbt[$d_b_t]['file'] ) ) {
      echo $dbt[$d_b_t]['file'] . ' on line ';
    }
    if ( isset( $dbt[$d_b_t]['line'] ) ) {
      echo $dbt[$d_b_t]['line'] . ' in a "';
    }
    if ( isset( $dbt[$d_b_t]['function'] ) ) {
      echo $dbt[$d_b_t]['function'] . '"<br>' . "\n";
    }
  }
  echo "<br>= = = = = = = = = = = = = = = = = = = = =<br>\n</div>";
//* * * * end of backtrace debug code * * *
Now I access the site with my browser.

The output looks something like this:

Quote

= = = = = = = = Backtrace = = = = = = = =
osc_template.php is referenced in F:\wamp\www\231\includes\application_top.php on line 447 in a "require"
F:\wamp\www\231\includes\application_top.php is referenced in F:\wamp\www\231\index.php on line 13 in a "require"

= = = = = = = = = = = = = = = = = = = = =

.
.
.
. (more output to the browser here)
.
.
.

= = = = = = = = Backtrace = = = = = = = =
osc_template.php is referenced in F:\wamp\www\231\includes\footer.php on line 37 in a "require"
F:\wamp\www\231\includes\footer.php is referenced in F:\wamp\www\231\includes\template_bottom.php on line 43 in a "require"
F:\wamp\www\231\includes\template_bottom.php is referenced in F:\wamp\www\231\index.php on line 274 in a "require"

= = = = = = = = = = = = = = = = = = = = =

Fatal error: Cannot redeclare class oscTemplate (previously declared in F:\wamp\www\231\includes\classes\osc_template.php on line 33)
Just above the "Cannot redeclare ..." error, in the first line of the backtrace is where I find where it is getting parsed more than once.

In my case:

Quote

osc_template.php is referenced in F:\wamp\www\231\includes\footer.php on line 37 in a "require"
That is exactly where I put the code below to purposely cause the error:

  require(DIR_WS_CLASSES . 'osc_template.php');
So I go to \includes\footer.php and I remove or "comment out" the offending code.

Then I access the site with my browser again to be certain the error is resolved.

When I'm sure the error is gone I remove the backtrace debug code.
:)
If I suggest you edit any file(s) make a backup first - I'm not perfect and neither are you.

"Headers already sent" - The definitive help

"Cannot redeclare ..." - How to find/fix it

SSL Implementation Help

Like this post? "Like" it again over there >

#2 tkpope

  • Community Member
  • 5 posts
  • Real Name:Sean Geraghty

Posted 24 June 2011, 17:58

This is the single-most helpful post I've seen on these forums!

Thank you so much!

#3 Sheepish

  • Community Member
  • 36 posts
  • Real Name:Martin

Posted 07 November 2011, 12:20

I was banging my head on the desk trying to sort out a problem and this helped me solve it in a couple of mins.

Thanks Germ!

#4 andes1

  • Community Member
  • 181 posts
  • Real Name:andrea pataquiva diaz

Posted 07 November 2011, 16:03

thanks