2

i have an issue with function redeclaration problem. So, i am trying to call multiple times this example.

class myClass {

    function abc() {

        function internal() {

        return "i am internal"; 
        }

    return internal();
    }

}


$myClass = new myClass();

echo $myClass->abc();
echo $myClass->abc(); // HERE IS THE PROBLEM when i call same function second time

PHP showing Fatal error: Cannot redeclare internal() (previously declared).

Does anyone have an idea how can i solve this issue?

Thanks in advance.

3
  • 2
    Don't define functions within other functions, there's no need for that.
    – Brad Koch
    Feb 8, 2013 at 15:23
  • 1
    Don't nest function definitions - there's no need to do it, it causes problems like this, and is a bad code smell
    – Mark Baker
    Feb 8, 2013 at 15:24
  • 1
    If you need internal to have access to variables local to abc then use Rocket's solution. Otherwise you should do something like what Lawrence suggested.
    – Jesse Vogt
    Feb 8, 2013 at 15:36

4 Answers 4

5

You cant redeclare the function period so try something like this:

<?php 
class myClass {

    function abc() {
        return $this->internal();
    }

    private function internal() {
        return "i am internal";
    }

}
?>
0
1

When you declare a function using function <name>(), you are not declaring it in the scope you think you are. That function is being declared in the class global scope, not the function/class scope.

See the PHP Docs: http://www.php.net/manual/en/functions.user-defined.php#example-149

So, when you call abc, you are re-declaring the global function, internal.

If you want to do what you are doing, you can use closures instead (NOTE: This only works in PHP 5.3+). This has the benefit of being able to read local variables from within abc (if you use the use keyword).

class myClass {

    function abc() {
        $internal = function() {
            return "i am internal"; 
        };

        return $internal();
    }
}

You can also declare internal as a private function of the class, especially if it's not going to change. Why keep re-making a function?

class myClass {

    private function internal(){
        return "i am internal"; 
    }

    function abc() {
        return $this->internal();
    }
}
0
1

Check whether the function exists prior to defining it:

class myClass {
    function abc() {
        if ( ! function_exists('internal')) {
            function internal() {
                return "i am internal"; 
            }
        }

        return internal();
    }
}
0

Alternatively:

if (!function_exists('internal')) { function internal ... }

However, you have to be aware that in this case, the function internal will be available outside the class scope as well. Lawrence's solution is a better approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.