Render a variable during creation of anonymous PHP function

后端 未结 1 1399
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-25 16:19

I\'m trying to get a simple sort function going using anonymous functions. One each for asc and desc sorting.

Is it possible to render the $sortBy vari

相关标签:
1条回答
  • 2021-01-25 16:40

    You can pass a variable in enclosing scope using the use keyword (Example #3 Closures and scoping):

    $sortBy = 'some_key';
    
    $sort['desc'] = function($x, $y) use ($sortBy) {
        // implementation
    };
    
    0 讨论(0)
提交回复
热议问题