Php 在用户定义函数中作为参数传递全局

Php 在用户定义函数中作为参数传递全局,php,Php,Matteo Tassinari在阅读有关将变量传递给用户定义函数的内容时表示,您可以使用“全局”访问函数外部的变量,但在查看答案时,他建议“完全避免使用全局变量,而更喜欢作为参数传递” 下面是我的索引 <?php $registry = new Registry(); <- need to pass this variable $router->map('GET', '/', function ($controller = 'Home', $action = 'index

Matteo Tassinari在阅读有关将变量传递给用户定义函数的内容时表示,您可以使用“全局”访问函数外部的变量,但在查看答案时,他建议“完全避免使用全局变量,而更喜欢作为参数传递”

下面是我的索引

<?php

$registry = new Registry(); <- need to pass this variable

$router->map('GET', '/', function ($controller = 'Home', $action = 'index') {

    if (method_exists($controller, $action)) {
        $controller = new $controller();
        $controller->$action();
    } else {
        echo 'missing';
    }
});

您可以尝试将
使用($registry)
添加到函数中

$router->map('GET', '/', function ($controller = 'Home', $action = 'index') use($registry) {

使用
使用
,谢谢您。。。我如何接受你的回答?