In PHP, what is a closure and why does it use the “use” identifier?

前端 未结 6 1978
醉酒成梦
醉酒成梦 2020-11-22 00:45

I\'m checking out some PHP 5.3.0 features and ran across some code on the site that looks quite funny:

public function getTotal($tax)
{
    $tot         


        
6条回答
  •  心在旅途
    2020-11-22 01:26

    This is how PHP expresses a closure. This is not evil at all and in fact it is quite powerful and useful.

    Basically what this means is that you are allowing the anonymous function to "capture" local variables (in this case, $tax and a reference to $total) outside of it scope and preserve their values (or in the case of $total the reference to $total itself) as state within the anonymous function itself.

提交回复
热议问题