简体   繁体   English

PHP中的闭包回调(Laravel 5)

[英]Closure callbacks in PHP (Laravel 5)

I am learning Laravel 5, and getting a little bit confused at these lines of code (creation of service provider): 我正在学习Laravel 5,这些代码行(创建服务提供者)有些困惑:

 public function register()
{
    $this->app->singleton('Riak\Contracts\Connection', function($app)
    {
        return new Connection($app['config']['riak']);
    });
}

I cant understand $app variable, and frome where it will be passed into function? 我无法理解$app变量,并且它将从哪里传递给函数? Thanks! 谢谢!

PS And please correct if I wrong, I read some posts, and how I understand closures is a anonimous functions what uses vars from outside scope (by using use () ), and lambda functions is just closure what doesnt use these vars. PS并且如果我错了,请更正,我阅读了一些文章,并且我了解闭包是一个从外部范围使用vars的匿名函数(通过使用use () ),而lambda函数只是不使用这些vars的闭包。 Or I miss something? 还是我想念什么?

$this->app->singleton ,它在调用时将$app传递给回调闭包。

The anonymous function is a Callable which is passed to the function singleton in the object $app . 匿名函数是一个Callable ,它将传递给对象$app中的singleton函数。

The function singleton then calls the Callable (anonymous function) that you passed to it, and passes $app as argument into the anonymous function. 然后,函数singleton调用您传递给它的Callable (匿名函数),并将$app作为参数传递给匿名函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM