Access variables from parent scope in anonymous PHP function

前端 未结 1 1631
陌清茗
陌清茗 2020-12-03 06:51

I want to write a function that does some dirty work logging a transaction, but the anonymous function scope does not seem to register the parent scope $db and

相关标签:
1条回答
  • 2020-12-03 07:34

    Use the use keyword to bind variables into the function's scope.

    function() use ($db) {
    

    Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header [using use].

    http://www.php.net/manual/en/functions.anonymous.php

    0 讨论(0)
提交回复
热议问题