nested functions in php throws an exception when the outer is called more than once

前端 未结 5 664
执念已碎
执念已碎 2021-01-19 18:35

lest assume that i have the following

function a(){
  function b(){}
}
a(); //pass
a(); //error

why in the second call an exception is thr

5条回答
  •  走了就别回头了
    2021-01-19 19:15

    This is because when you execute function a it declares function b. Executing it again it re-declares it. You can fix this by using function_exists function.

    function a(){
      if(!function_exists('b')){
        function b(){}
      }
    }
    

    But what I suggest is, you should declare the function outside. NOT inside.

提交回复
热议问题