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

前端 未结 6 1986
醉酒成梦
醉酒成梦 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:33

    Until very recent years, PHP has defined its AST and PHP interpreter has isolated the parser from the evaluation part. During the time when the closure is introduced, PHP's parser is highly coupled with the evaluation.

    Therefore when the closure was firstly introduced to PHP, the interpreter has no method to know which which variables will be used in the closure, because it is not parsed yet. So user has to pleased the zend engine by explicit import, doing the homework that zend should do.

    This is the so-called simple way in PHP.

提交回复
热议问题