PHP - Function inside a Function. Good or bad?

后端 未结 4 1481
有刺的猬
有刺的猬 2020-12-30 01:41

I would like to know if it is a good thing to define a function inside another function in PHP. Isn\'t it better to define it before the function (and not inside) in terms o

4条回答
  •  没有蜡笔的小新
    2020-12-30 02:14

    It depends on the situation, as it may be more desirable than using create_function(). However you should know that the function which is created within the function is global in scope.

    function creator() {
    
        function inside() {
    
            echo "hi.";
        }
    }
    
    creator();
    inside();
    

    This will print "hi." even though the inside() function was created "inside" of the creator function. So if you have a function in a loop which is creating a function, you need to check to see if the function exists, otherwise it will cause a function exists error after the first loop.

提交回复
热议问题