Jump to content


Corporate Sponsors


Latest News: (loading..)

germ

Member Since 10 Apr 2007
Offline Last Active Feb 08 2012, 11:14
*****

Topics I've Started

"Cannot redeclare ..."

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.
:)

"Headers already sent..."

12 June 2011, 22:32

"Headers already sent" help contribution

[contribution]Store Open/Closed Support Thread

11 June 2011, 22:00

This contribution allows you to set days and times your store is "open" and "closed".

If your store is not "open" it prevents customers from checking out.

Coded for 2.3.1 but easily adapted to 2.2 versions.

Link to contribution forthcoming.

If you post in this thread and get no response in a reasonable amount of time (say 24 hours), PM me.



Contribution link: Store Open/Closed