drlhsfqoa350437979 2016-10-28 05:17
浏览 40
已采纳

如何用额外的参数解决laravel容器中的类?

How to resolve a class in laravel container with extra arguments?

This works perfect and without any registration:

class myClass
{
    public function __construct(Interf1 $i1, Interf2 $i2)
    {
        //some stuff
    }
}
$mc = app()->make('myClass');

What happends if you need something like this:

class myClass
{
    public function __construct(Interf1 $i1, Interf2 $i2, $x, $y)
    {
        //some stuff
    }
}

Is there an option like:

$mc = app()->make('myClass',[10,20]);

or

$mc = app()->make('myClass')->with(10,20);

I know I can use setters but thats not the point..

  • 写回答

1条回答 默认 最新

  • dtq26360 2016-10-28 06:14
    关注

    yes it is exactly the first version you already wrote, but you missed the keys which must match the arguments in the constructor:

    $mc = app()->make('myClass',['x' => 10, 'y' => 20]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭
  • ¥15 Matlab读取根元素出错