038-PHP closure function the returned instance, the variable external parameters passed

? < PHP
 #   to return the closure function example, external variables passed argument 
# direct call will not output the contents of $ TXT 
    function Demo () {
         $ TXT = 'I love the PHP' ; 

        # . 1, function () in variables, parent class instance may be transmitted outside the 
        # variable 2, variable (in) use, as an internal instance of the parent class instance can be transferred 
        $ FUNC = function ( $ STR = '') use ( $ TXT ) {
             echo  $ TXT ;
             echo 'a' ;
             echo  $ STR ; 
        }; 
        # here no direct calls, and returns an instance is to 
        return  $ FUNC ; 
    } 

#Test the 
$ RES = Demo ();           // function returns an instance of 
$ obj = $ RES ( 'I love Delphi');   // through the res () call, without this step, will not be output TXT $ 
>?

 

Guess you like

Origin www.cnblogs.com/tianpan2019/p/10993525.html