PHP Fatal error: Cannot redeclare

October 18, 2010

” Fatal error: Cannot redeclare [function-name]() … ”

I defined a function (fff) in the top of my pgm. (not in a loop)
it spanned from line 5 to 11 and used it in the program, later.
I got this error msg:

Fatal error: Cannot redeclare fff() (previously declared in
/…/xxx.php:5) in
/…/xxx.php on line 11

clearly, I only defined it once in the top of the program, not twice.
This has never been fixed.


Solving the Problem:

I then removed it to an external file (fff-functions.php) and did a
“require_once() for xxx-functions.php on line 6 and got these 2
error messages where there should have only been one.
IT WAS TRYING TO LOAD THE FUNCTION(PGM) TWICE:

Warning: require_once(xxx-functions.php) [function.require-once]:
failed to open stream: No such file or directory in
/…/xxx.php on line 6

Fatal error: require_once() [function.require]: Failed opening required
‘xxx-functions.php’ (include_path=’.:/usr/share/pear’) in
/…/xxx.php on line 6

this happened within 2 checkout pages of zencart “part 3 of 3”
and on the login page

PPS
I now remember a few months ago I added code to a pgm I had written
to access another $_POST variable. I also moved the call to my program
(in includes/modules/pages/checkout_confirmation/header_php.php) down
past some error checking. I immediately started getting “Fatal error:”
messages about things being defined twice throughout many (untouched) zc pgms.:
i ‘undid’ things but could not stop it until I had
changed all the require() calls to require_once()
===================================================

————————————————————————-
Warning: require_once(xxx-functions.php) [function.require-once]:
failed to open stream: No such file or directory in
/…/xxx.php on line 6
Fatal error: require_once() [function.require]: Failed opening required
‘xxx-functions.php’ (include_path=’.:/usr/share/pear’) in
/…/xxx.php on line 6
————————————————————————-

Fatal error: Cannot redeclare class order in /…/includes/classes/order.php on line 1018
Fatal error: Cannot redeclare class shipping in /…/includes/classes/shipping.php on line 178
Fatal error: Cannot redeclare class order_total in /…/includes/classes/order_total.php on line 232
Fatal error: Cannot redeclare class payment in /…/includes/classes/payment.php on line 255
Fatal error: Cannot redeclare class cc_validation in /…/includes/classes/cc_validation.php on line 184
and more.

again, it has to be “php vs. pear”
this was in “PHP Version 5.1.6”. From the comments above, it must
still be in 5.2.4

system notes:
zencart Version 1.3.8a
Server OS: Linux 2.6.18-53.1.14.el5.centos.plus
HTTP Server: Apache/2.2.3 (CentOS)
PHP Version: 5.1.6 (Zend: 2.1.0)

Tenative Conclusion: php has already loaded and defined everything.
pear should not be re-loading or re-defining anything.
this is the conflict.
[the possibility of any exception(s) is the source of the conflict]

===============
2009 workaround

change require() to require_once()

I thought I found someone reporting the same problem:
BUG REPORT TO PHP on Feb. 4th 2009   http://bugs.php.net/bug.php?id=18590
but, looking at it more closely now, I think not and so it may be that the error is still un-reported.


2010

I installed Fedora 13 with PHP 5.3 on Sept. 2010.
I had been running Fedora 11 with PHP 5.2 till then.

on my pc I have a number of (virtual host) development copies of web sites. 2 of them are commercial and run zen-cart. Of the 2, one has been fine. I activated the 2nd one today, 2010-10-18, and discovered it would only display a blank page. Turning on @ini_set(‘display_errors’, ‘1’); error_reporting(E_ALL ^ E_NOTICE); revealed, (along with hundreds of Deprecated: Function ereg() messages), this:

/…/[zencart]/includes/classes/db/mysql/query_factory.php on line 412 Fatal error: Cannot redeclare date_diff() in /…/[zencart]/includes/functions/functions_general.php on line 1476

===============
2010 workaround

surround function with:

if (! function_exists(function-name) ) {
<<< function function-name() { } >>>
}

this example:

// compute the days between two dates
 if (! function_exists(date_diff)) {
  function date_diff($date1, $date2) {
  //$date1  today, or any other day
  //$date2  date to check against

    $d1 = explode("-", $date1);
    $y1 = $d1[0];
    $m1 = $d1[1];
    $d1 = $d1[2];

    $d2 = explode("-", $date2);
    $y2 = $d2[0];
    $m2 = $d2[1];
    $d2 = $d2[2];

    $date1_set = mktime(0,0,0, $m1, $d1, $y1);
    $date2_set = mktime(0,0,0, $m2, $d2, $y2);

    return(round(($date2_set-$date1_set)/(60*60*24)));
  }
 }

Leave a Reply

We try to post all comments within 1 business day