Jump to content
Latest News: (loading..)

Archived

This topic is now archived and is closed to further replies.

germ

"Cannot redeclare ..."

Recommended Posts

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:

 

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:

 

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:

 

= = = = = = = = 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:

 

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 >

Share this post


Link to post
Share on other sites

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:

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


For a new install or if your store isn't mobile-friendly, get the community-supported responsive osCommerce (2.3.4.1 CE) here: https://github.com/gburton/Responsive-osCommerce/archive/2341-Frozen.zip

Working on generalising bespoke solutions for Quickbooks integration, Easify integration and pay4later (DEKO) integration at 2.3.x

Share this post


Link to post
Share on other sites

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:


:blink:
osCommerce based shop owner with minimal design and focused on background works. When the less is more.
Email managment with tracking pixel, package managment for shipping, stock management, warehouse managment with bar code reader, parcel shops management on 3000 pickup points without local store.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 >

Share this post


Link to post
Share on other sites

×