anonymous functions (lambdas, closures) in PHP 4

前端 未结 2 1392
清歌不尽
清歌不尽 2021-01-22 20:01

Is there a trick in PHP 4 to implement functions which return functions? I expected that the following code would work:

function xxx($a) {
  return function($b)          


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-22 20:34

    You're probably barking at the wrong tree. PHP is not a functional programming language. Some changes have been made starting with PHP 5.3, but even there you don't have your expected variable scopes that would allow you to do what you have in your examples.

    The only tools you can use in PHP 4 are create_function and some ingenuity to write the function definition as a string.

    
    

    ...but even if this is simple for your posted example, it definitely isn't practical for more complex situations.

提交回复
热议问题