문제

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