简体   繁体   中英

symfony2 container anonymous functions

Is it possible to use anonymous functions to register factories in symphony2?

For example:

use Foo\Baz;
use Symfony\Component\DependencyInjection\Definition;

$baz = new Definition(Baz::class);

$baz->setFactory(function(){
    return new Baz();
});

$container->setDefinition(Baz::class, $baz);

您描述的功能自Symfony v2.6.0起已可用-但是您显然无法通过XML或YAML设置闭包/匿名函数,因此设置闭包工厂的最佳位置可能是在包的扩展类中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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