php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18590 Fatal error: Cannot redeclare upon reload
Submitted: 2002-07-26 07:35 UTC Modified: 2003-12-04 02:24 UTC
Votes:64
Avg. Score:4.2 ± 1.1
Reproduced:55 of 57 (96.5%)
Same Version:20 (36.4%)
Same OS:20 (36.4%)
From: armageddon at raydan dot de Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.0.0-dev OS: WinXP Pro
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2002-07-26 07:35 UTC] armageddon at raydan dot de
When I run the icq_main.php script, after some seconds I get following two errors:

Fatal error: Maximum execution time of 5 seconds exceeded in j:\www\public\phpicq\icq\icq_main.php on line 48

Fatal error: Maximum execution time of 5 seconds exceeded in j:\www\public\phpicq\icq\icq_debug.php on line 121

Ok, thats the excepted behaviour, but when I reload the page I get this strange error:

Fatal error: Cannot redeclare hexdump() (previously declared in :67) in j:\www\public\phpicq\icq\icq_debug.php on line 71

When I reload it again I get the first two error-messages again and so on...

The following code is from icq_debug, Line 67-71:

  function HexDump($in) {
      static $hex;
      if (!$hex) $hex = new Hex();
      return $hex->Dump($in);
  }

I have loaded the following modules:
php_bz2.dll, php_cpdf.dll, php_crack.dll, php_db.dll, php_dba.dll, php_dbase.dll, php_exif.dll, php_filepro.dll, php_gd.dll, php_gettext.dll, php_hyperwave.dll, php_ldap.dll, php_mhash.dll, php_pdf.dll, php_pgsql.dll, php_shmop.dll, php_snmp.dll, php_sockets.dll, php_w32api.dll, php_zlib.dll

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-29 01:34 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2003-12-04 02:24 UTC] sniper@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 [2004-03-31 09:38 UTC] christian dot lefebvre at atosorigin dot com
I got the same problem with RC1 version under
Linux/apache2filter
  I just tried with snapshot 200403311230 (same config)
and the message disappear but the tested code still crashes.
  As the same code works with php4.3.4, I think this ticket
should be reopened.

  I'll try to make a smaller test case
 [2004-03-31 09:57 UTC] christian dot lefebvre at atosorigin dot com

 [2007-09-03 13:48 UTC] info at rchu dot nl
I have sort of the same bug I think.

test.php
<?php

function test() { echo 'test'; }

test();

?>

works fine the first time. Then after a reload it gives an cannot redeclare test() error. Same error after second reload etc. Modify the file (add a space or something like that) and it runs OK the first time but after reload same error.

I have compiled PHP 5.2.4 on Darwin Kernel Version 8.10.2.
 [2007-09-03 13:52 UTC] info at rchu dot nl
update: adding a space to the source results in the same 'cannot redeclare' error. Only changing the function name resolves the problem at the fist run. Must be some kind of caching problem I suppose. When running the script on the command line there is no problem, just when I request in in my browser. I run the pre-installed apache1.3 on Mac OS X 10.4.10 and used the --with-apache-hooks option on compiling because I do not have the apache.h source file (since I haven't compiled apache).
 [2008-05-30 12:43 UTC] mark dot php dot net dot special at emceesoftware dot com
PHP 5.2.4 (Build date 30 Aug 2007)

I experience this error while using a technique for dispatching method calls dynamically.

On receipt of an XML packet, an API method determines the php module (source file) that needs to be loaded to process the XML file.

Next, the method name is loaded into a variable, and the source file is included.

Finally, the variable is used to call the method.

I've been successfully testing this approach for some time, and am confused as to why it has started "now".

Code sample (abbreviated):

$moduleName = ... (extracted from XML structure)
$methodName = ... (derived from another parameter)
$sourceFile = $_SERVER["DOCUMENT_ROOT"].'/path/'.$moduleName.'.php';
include( $sourceFile );
$dynamicMethod = $moduleName.'_'.$methodName;
$dynamicMethod();  // attempt the call

My theory is that this code is being called multiple times with the same value for $moduleName, causing the include() method to attempt to load it multiple times.  While this doesn't seem to be an issue when using the static 'include_once' declaration, it appears to be an issue when loading dynamically.

When changing the above code to use include_once(), the problem seems (so far) to have gone away.
 [2008-05-30 13:48 UTC] mark dot php dot net dot special at emceesoftware dot com
As a followup to my last comment, the way this "error" is being reported proved a little confusing, and my warrant some minor research in order to help make it a bit easier to identify when it happens.

My error log reports the following (abbreviated):

PHP Fatal error:  Cannot redeclare simplehtml_rendercontent() (previously declared in SimpleHTML.php:149) in SimpleHTML.php on line 152

Noteworthy is the fact that the RenderContent() method is declared on line 149.  The closing brace for the method is on line 152.

It was this difference in line numbers that proved the most confusing.  If they both reported out at line 149, I suspect I would have more quickly determined that I was seeing a reload problem of some sort.

Also, if after adjusting the line number being reported, it is determined that they are identical, it might be helpful to post an additional line in the error log which suggests the developer look at replacing an include() with include_once() or require() with require_once().

With that bit of reporting, I suspect most folks who get bit with this type of issue will be able to resolve it more quickly (whether dynamically as I've done, or at the top of a PHP file).
 [2009-02-04 02:59 UTC] greg at pwsdb dot com
this experience/test may help:

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 code below.  
I got this error msg:


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


I then removed it to an external file (xx-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
 [2009-02-04 03:46 UTC] greg at pwsdb dot com
PS
This only happened within a checkout page of ZenCart ("part 3 of 3")  
and not another (not the login page) ... I don't know enough about zc
to know what the significant difference is.
 [2009-02-04 05:22 UTC] greg at pwsdb dot com
PPS
no, it is not working in the login page now, either.
Same experience as the others above?:  
"First it works, then it doesn't."
 [2009-02-04 17:22 UTC] greg at pwsdb dot com
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() 

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  Ver. 5.2.4
 [2009-02-04 17:31 UTC] greg at pwsdb dot com
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)
 [2009-02-04 20:44 UTC] greg at pwsdb dot com
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]
 [2010-01-28 09:27 UTC] krajicek at centrum dot sk
confirming the same (similar) issue - if file with function definition is included twice (via include) in the script run it throws Fatal Error - Cannot redeclare functionXY. on reload the Fatal Error is gone. on any change to script or PHP related server config is error back again. fix for the error message is to use include_once instead of include.
but this can cause problems by not displaying error messages all the times.

OS: FreeBSD 6.3-STABLE-20080418.1500
Apache 2.0 Handler (20051115)
PHP Version 5.2.12
 [2010-01-28 16:54 UTC] krajicek at centrum dot sk
...adding some more comments to my above.

here is the simplest sample i've created:

redeclare_issue_main.php:
-----------------------------
<?php
include ('redeclare_issue_test.inc.php');
include ('redeclare_issue_test.inc.php');

sendMail1();
?>
-----------------------------


redeclare_issue_test.inc.php:
-----------------------------
<?php
function sendMail1()
{
  echo 'test';
}
?>
-----------------------------

running the redeclare_issue_main.php code after any change (e.g. add blank line) to redeclare_issue_test.inc.php will bring up:

Fatal error: Cannot redeclare sendmail1() in redeclare_issue_test.inc.php on line 2

upon browser reload the error is gone.
cannot confirm my previous assumption that also change in PHP related server config cause that the error reappears.

OS: FreeBSD 6.3-STABLE-20080418.1500
Apache 2.0 Handler (20051115)
PHP Version 5.2.12
 [2013-07-25 15:41 UTC] ukvuidivud at sfkilr dot com
zzylhcvht/qiq/ofu, <a href="http://clomidid.com/">Clomid statics</a>, hsRyJpJ, [url=http://clomidid.com/]Drug clomid[/url], ZQCCmHw, http://clomidid.com/ Clomid, NQgkNdv.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sun Apr 20 15:01:54 2014 UTC