Php $this在此SLIM代码中包含哪个对象引用?

Php $this在此SLIM代码中包含哪个对象引用?,php,twig,slim,Php,Twig,Slim,我正在尝试使用SLIM的twig视图呈现一个简单的HTML视图。我想知道当我使用get函数进行路由时 //Get container $container = $app->getContainer(); //Register Component on container $container['view'] = function($container) { $view = new \Slim\Views\Twig('templates', [ 'cache' =>

我正在尝试使用SLIM的twig视图呈现一个简单的HTML视图。我想知道当我使用get函数进行路由时

//Get container
$container = $app->getContainer();

//Register Component on container

$container['view'] = function($container) {
    $view = new \Slim\Views\Twig('templates', [
    'cache' => false
    ]);

    $view->addExtension(new \Slim\Views\TwigExtension(
    $container['router'],
    $container['request']->getUri()
    ));

    return $view;

};

$app->get('/', function(Request $request, Response $response) {
    return $this->view->render($response, 'index.html');
});

$app->run();
那么$this包含哪个对象引用。 请帮我澄清一下。谢谢

$这是slim的依赖项注入容器:

在中,您可以看到slim使用闭包上的函数来设置$this实例。

$this指向创建闭包的类

class a {
    public function test(){
       //closure is defined in the class
       return function(){
            print_r($this);//so you have access to $this
       };
    }
}

$a = new a();
$a->test();
在您的示例中,您正在使用,但它不是这样工作的。但它基本上在php中工作。

来自:

注意,在组闭包中,使用$this代替$app。苗条的 为您将闭包绑定到应用程序实例,就像这样 路由回调的案例与容器实例绑定

在组闭包内,$this绑定到Slim\App的实例 在route closure内部,$这被绑定到Slim\Container的实例
试着扔掉这个美元;死亡它正在给Slim\Container。但是现在我想问这个闭包函数是如何访问容器对象的。我的意思是,我还没有传递它,甚至没有包括闭包引用容器对象的用法。请帮助我,现在我想问这个闭包函数是如何访问容器对象的。我的意思是,我还没有传递它,甚至没有包括闭包引用容器对象的用法。请帮助我,这意味着当我们调用clousure作为参数时,Slim正在返回它自己的闭包,它自己决定引用的上下文。是不是@jmattheisNo slim在使用$app->post,get或其他任何东西时使用您给出的闭包,并使用bindTo@jmattheis我用这个词似乎注意到了。