2

I'm migrating a PHP 5.2.x application to a new 5.2.x server. The old server actually started as a PHP 4.0 server many years ago and was upgrade to PHP 5.2 over time. One of our modules has a function that gets redeclared if this module is used more than once. We can easily fix this, but we're perplexed at how it ever could have worked.

On the new server it will fail with an expected:

Fatal error: Cannot redeclare function

The problem is that on the old server it was always re-declaring the function! Is there a PHP setting or special usage being used here that makes it work on one server but not another?

Thank you!

Edit Still trying to pour through how this is possible. The site FATAL errors but has execution after that point of error.

Charles
  • 50,943
  • 13
  • 104
  • 142
Kristopher Ives
  • 5,838
  • 7
  • 42
  • 67
  • 2
    Are you sure? Without some **serious** [hackery](http://www.php.net/manual/en/ref.runkit.php), you can't ever redeclare a PHP function. – ceejayoz Mar 26 '12 at 20:52
  • 4
    PHP4 allowed function redeclations, PHP5 doesn't. you'll have to change the module to an include_once/require_once, or wrap the function definition in a `if (!function_exists('...')) { ... }` block. – Marc B Mar 26 '12 at 20:52
  • @MarcB Have I really been using PHP5 that long? I could swear PHP4 doesn't allow it either. – ceejayoz Mar 26 '12 at 20:53

2 Answers2

1

Redeclaring functions is consider a error.

Maybe you guys can use "rename function".

http://es.php.net/manual/en/function.rename-function.php

if(function_exist("foo")){
  rename_function('foo', 'old_foo' );

  function foo(){
        /*...*/
  }
}

Another idea is to rewrite code to do this

$foo = function(){  /* something */....  };

So the next time you want to redefine $foo(), you do

$foo = function(){  /* something else */....  };
Tei
  • 1,400
  • 8
  • 6
0

I am unable to reproduce your description, PHP 4 does not allow you to redeclare functions:

echo PHP_VERSION;
function foo() {}
function foo() {}

Demo/Output:

4.4.9
Fatal error: Cannot redeclare foo() (previously declared in /homepages/26/d94605010/htdocs/lz/writecodeonline.com/php4/index.php(138) : eval()'d code:2) in /homepages/26/d94605010/htdocs/lz/writecodeonline.com/php4/index.php(138) : eval()'d code on line 3

You must mix things here, so better find out more about facts when debugging, not guessing (yes I know can be hard sometimes, but facts help when debugging, aim for them).

And if it's a fatal error, your script ends. You can add a shutdown callback function to further debug your issue, see a related question:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836