Ben ne yazık ki, PHP 5.2 böyle bir şey yapabilir sanmıyorum - ama PHP 5.3 kullandığınız gibi ... Eğer bu işe almak için Kapaklar kullanabilirsiniz.
To begin, here's a quick example of using a Closure :
function foo()
{
$l = "xyz";
$bar = function () use ($l)
{
var_dump($l);
};
$bar();
}
foo();
Görüntüler:
string 'xyz' (length=3)
use anahtar fark ;-)
And here's an example of how you could use that in your specific case :
function compilePath( $route )
{
preg_replace_callback( '$:([a-z]+)$i', function ($matches) use ($route) {
var_dump($matches, $route);
} , $route['path'] );
}
$data = array('path' => 'test:blah');
compilePath($data);
Ve bu çıktıyı almak istiyorum:
array
0 => string ':blah' (length=5)
1 => string 'blah' (length=4)
array
'path' => string 'test:blah' (length=9)
Birkaç not:
- Ben kullanılan
preg_replace_callback , and not preg_replace - Ben denilen bazı geri çağırma işlevi istediğiniz gibi.
- Ben geri arama gibi bir anonymous function istimal
- And that anonymous function is importing
$route, with the new use keyword.
- Hangi benim geri arama işlevi, her zaman geri çağırma fonksiyonu
preg_replace_callback tarafından geçirilen maçları, ve $route hem de erişebileceğiniz anlamına gelir.