Jump to content



Latest News: (loading..)

- - - - -

"Cannot redeclare ..."


  • Please log in to reply
7 replies to this topic

#1   germ

germ
  • Members
  • 13,921 posts
  • Real Name:Jim
  • Gender:Male
  • Location:USA (GMT-6)

Posted 19 June 2011 - 06:44 PM

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.

"Given enough impetus a parallelogramatically shaped projectile can egress a circular orifice."
- Me -

"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

tkpope
  • Members
  • 5 posts
  • Real Name:Sean Geraghty

Posted 24 June 2011 - 05:58 PM

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

Thank you so much!

#3   Sheepish

Sheepish
  • Members
  • 36 posts
  • Real Name:Martin

Posted 07 November 2011 - 12:20 PM

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

andes1
  • Members
  • 181 posts
  • Real Name:andrea pataquiva diaz

Posted 07 November 2011 - 04:03 PM

thanks

#5   JohnAtYM

JohnAtYM
  • Members
  • 66 posts
  • Real Name:John Ferguson
  • Gender:Male
  • Location:sarf Landin

Posted 02 October 2012 - 10:13 PM

Just spent a while resolving one of these errors and the above technique didn't find the problem. It turned out that there wasn't only one file declaring the class, but included more than once.

I had inadvertently copied a module declaration into one of the language sub-directories, so there were two copies of the same file (in different directories) both being included, but unfortunately it was the proper file reporting the error.

I put the following code into the file where the class was supposed to be declared to find out the name of the other file erroneously containing a declaration:

	$myclass = 'ht_canonical';
	$reflector = new ReflectionClass($myclass);
	echo 'Class ' . $myclass . ' defined in ' . $reflector->getFileName();


In my case, the error had been:

Quote

Fatal error: Cannot redeclare class ht_canonical in....

Currently working for clients on: 2.3 implementation, migration to 2.3 from 2.2, development on 2.2 & 2.3
2.3 add-ons installed: Quick UK Localisation, Modular Front Page, Dynamic Template System, Separate Price Per Customers, Hide Products for SPPC, Theme Switcher, Extra Product Fields, Ultimate SEO URLs
In development: osc facebook app for 2.3, eBay XML API for 2.3 - Like to see/would collaborate in: Googlecheckout for 2.3

#6   Gergely

Gergely
  • Community Team
  • 528 posts
  • Real Name:Gergely Tóth
  • Gender:Male

Posted 03 February 2013 - 06:54 AM

Dont forget this controll in bm_languages.php class.

	function execute() {
	  global $PHP_SELF, $lng, $request_type, $oscTemplate;
..
		if (!isset($lng) || (isset($lng) && !is_object($lng))) {
		  include(DIR_WS_CLASSES . 'language.php');
		  $lng = new language;
		}
...

the $lng can not redeclared if declared before. Use if() statments for similar problems. :thumbsup:
Header Footer Content Modules
SCM
v3

and some rewrites :-)

#7   partrade12

partrade12
  • Members
  • 20 posts
  • Real Name:Erin

Posted 26 March 2013 - 05:29 PM


Below is my code i got after inputing the back trace, i found the code on line 130, what do i do next?! What exactly do i change or not change?



= = = = = = = = Backtrace = = = = = = = =
customer_greeting.php is referenced in ............................................/modules.php on line 130 in a "include"

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


Fatal error: Cannot redeclare class customer_greeting in .......................................................customer_greeting.php on line 34

#8   germ

germ
  • Members
  • 13,921 posts
  • Real Name:Jim
  • Gender:Male
  • Location:USA (GMT-6)

Posted 26 March 2013 - 10:52 PM

Put the backtrace code in /includes/classes/customer_greeting.php and try it again.

That's the file getting referenced twice that's causing the problem.
If I suggest you edit any file(s) make a backup first - I'm not perfect and neither are you.

"Given enough impetus a parallelogramatically shaped projectile can egress a circular orifice."
- Me -

"Headers already sent" - The definitive help

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

SSL Implementation Help

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