Search code examples
phpphp4

PHP4 - "Fatal error: Cannot redeclare function()"


Second time I've ran into this...

I have function foo() that has a helper function inside called formatStr() which is used in an array_map() call. When I call foo() more than once within a script I get a "Fatal error: Cannot redelcare formatStr()". Which leads me to believe formatStr() is not declared locally in the function but globally. Is this true? Can you get around this with function_exists()?

Thanks


Solution

  • You have a function defined within the foo() function? If so, move it out.

    Otherwise, just wrap formatStr() within function_exists()...

    if (!function_exists('formatStr'))
    {
        function formatStr()
        {
            // Your function code
        }
    }