質問

I noticed that PHP now can use temp function definition (like js and other scripts language), but how can I get local variable in the temp function ?

ex:

function my_func($text, $prefix){
    $regex = '/xxxx/';
    return preg_replace_callback($regex, function($matches){
         // how to read $prefix here?
     }, $text);
}
役に立ちましたか?

解決

The use will do the job..

function my_func($text, $prefix){
    $regex = '/xxxx/';
    return preg_replace_callback($regex, function($matches) use(&$prefix){ 
                                                          //^^^^^^^^^^^^^
     }, $text);
}

他のヒント

I find it ..

http://www.php.net/manual/en/functions.anonymous.php

function my_func($text, $prefix){
    $regex = '/xxxx/';
    return preg_replace_callback($regex, function($matches) use ($prefix){
         // how to read $prefix here?
     }, $text);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top