是否可以用这样的代码重构匿名函数:
function foo($path, $callback) {
$callback();
}
$app = array('a', 'b', 'c');
foo('vehicle/:id', function() use ($app) {
echo $app[0];
});
我试过这个,但它没有回应任何东西:
function foo($path, $callback) {
$callback();
}
$vehicleCallback = function() use ($app) {
echo $app[0];
};
$app = array('a', 'b', 'c');
foo('vehicle/:id', $vehicleCallback);
其他变体给了我语法错误。如果这很重要,我希望将这些函数移动到一个单独的文件中。
这是我的最终目标
回调.php
$cb1 = function () use ($app) {
// things
};
$cb2 = function () use ($app) {
// things
};
// ...more callbacks
路由器.php
require 'callbacks.php';
$app = new \Foo();
// code that might possibly manipulate $app
$app->bar('/some/relative/path/:param1', $cb1);
$app->bar('/another/relative/path', $cb2);
// possibly more code that mutates $app and then more callbacks