Question

Recently stumbled upon this neat little bug or 'feature' in PHP:

function myCmpFunc($a,$b) {
    function inner($p) {
         // do something
    }
    $inner_a = inner($a);
    $inner_b = inner($b);
    if ($inner_a == $inner_b) return 0;
    return ($inner_a > $inner_b ? -1 : 1);
}

Results in a fatal error "cannot redeclare function inner in ...", when called like this

usort($myArray, 'myCmpFunc');

It works flawlessly when function inner is declared outside of myCmpFunc and/or $myArray has not more than 2 elements ;)

-- edit --

somehow Related: PHP Fatal error: Cannot redeclare function

So here is my question, then: Is it possible to declare functions in local scope?

-- edit 2 --

Maybe, this works well in PHP 5.3 just read it has closures, yeehaa!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top