简体   繁体   English

Silex服务 - $ app参数或“use($ app)”声明?

[英]Silex service - $app parameter or “use ($app)” statement?

If I define a service in Silex application, I can either expect the main container (Application) to be passed as a parameter or can take it from the current scope using "use ($app)" statement. 如果我在Silex应用程序中定义一个服务,我可以期望主容器(Application)作为参数传递,或者可以使用“use($ app)”语句从当前作用域中获取它。

The official documentation at http://silex.sensiolabs.org/doc/services.html has this snippet: http://silex.sensiolabs.org/doc/services.html上的官方文档包含以下代码:

$app['some_service'] = function ($app) {
     return new Service($app['some_other_service'], $app['some_service.config']);
};

But it can be equally written as 但它可以同样写成

$app['some_service'] = function () use ($app) {
     return new Service($app['some_other_service'], $app['some_service.config']);
};

and I've seen a lot of examples of such code. 我已经看过很多这样的代码示例。 Is it just personal taste or one of them has benefits over another, such as in performance, memory usage or code isolation? 它只是个人品味还是其中一种比其他产品更有优势,例如性能,内存使用或代码隔离?


Edit: I've run perfromance comparison of "use" vs parameter vs simple function: 编辑:我运行了“使用”与参数与简单函数的性能比较:

$func = function () use ($app) {...};
$func = function ($app) {...};
function test($app) {...}

The first one is the slowest, but overall difference is less than 25%. 第一个是最慢的,但总体差异小于25%。 And as any micro-optimization, it's noticeable only if you have tens of thousands of these as the time difference between the fastest and slowest was about 1/10,000,000th of a second (0.05s for 1 million reps). 并且作为任何微优化,只有当你有成千上万的这些时才会引人注意,因为最快和最慢之间的时差约为1 / 10,000,000秒(100万代表为0.05)。

So performance difference should not be considered. 因此不应考虑性能差异。

Here is a direct answer: there is no practical difference. 这是一个直接的答案:没有实际的区别。 The only scenario I can see where use ($app) wouldn't work is if you define some entries in separate files. 我可以看到use ($app)不起作用的唯一情况是在单独的文件中定义一些条目。

What follows is just if you are curious ;) 以下是你好奇的事情;)

The advantage of passing the container again in the parameters is that it allows Silex to pass another container instance instead. 在参数中再次传递容器的优点是它允许Silex传递另一个容器实例。 Silex doesn't do that, so it doesn't affect you. Silex不这样做,所以它不会影响你。 But some other containers do. 但其他一些容器呢。

The main reason for a container to do that would be if you have a complex setup with multiple containers that are chained one after another: ParentContainer -> SubContainer . 容器执行此操作的主要原因是,如果您有一个具有多个容器的复杂设置,这些容器一个接一个地链接: ParentContainer - > SubContainer When you define an entry in the SubContainer , you want to be able to fetch dependencies from the parent container (which will automatically look in the subcontainer too). SubContainer定义条目时,您希望能够从父容器中获取依赖项(它将自动查看子容器中的依赖项)。 So the subcontainer will pass the parentcontainer in the closure parameter. 因此,子容器将在closure参数中传递parentcontainer。

That's an advanced (and rare) use case, but it's interesting to know ;) 这是一个高级(和罕见)用例,但知道它很有趣;)

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

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