尝试进入 Laravel 依赖注入

singleton(ClassInterface::class, T1Impl::class);$out1 = ap

尝试进入 laravel 依赖注入

<?php use AppModelsUser;

test('Action', function () {

    app()->singleton(ClassInterface::class, T1Impl::class);
    $out1 = app(ClassInterface::class)();
    $this-&gt;assertEquals("T1?", $out1);

    app()-&gt;bind(ClassInterface::class, T2Impl::class);

    $out2 = app(ClassInterface::class)();
    $this-&gt;assertEquals("T2!", $out2);

    app()-&gt;bind(T1Impl::class, T2Impl::class);
    $out3 = app(T1Impl::class)();
    $this-&gt;assertEquals("T2!", $out3);



    app()-&gt;bind(T2Impl::class, T3Impl::class);
    app()-&gt;bind(T1Impl::class, T2Impl::class);
    app()-&gt;singleton(ClassInterface::class, T1Impl::class);
    $out4 = app(ClassInterface::class)();
    $this-&gt;assertEquals("T3!", $out4);


    $user = User::factory()-&gt;create(['name'=&gt;'Tomas']);
    $out5 = app(T4::class, ['user'=&gt;$user])();
    $this-&gt;assertEquals("Tomas", $out5);
});

interface ClassInterface {
    public function __invoke();
}

class T1Impl implements ClassInterface {
    public function __invoke() {
        return "T1?";
    }
}

class T2Impl implements ClassInterface {
    public function __invoke() {
        return "T2!";
    }
}


class T3Impl implements ClassInterface {
    public function __invoke() {
        return "T3!";
    }
}

class T4 {
    public function __construct(
        public User $user
    ){}

    public function __invoke() {
        return $this-&gt;user-&gt;name;
    }
}

登录后复制

并且..这意味着如果我“正确”地执行laravel,它可以让我删除“nwidard/larave-modules”模块及其覆盖代码的任何部分?

以上就是尝试进入 Laravel 依赖注入的详细内容,更多请关注叮当号网其它相关文章!

文章来自互联网,只做分享使用。发布者:weapp,转转请注明出处:https://www.dingdanghao.com/article/715140.html

(0)
上一篇 2024-08-12 18:56
下一篇 2024-08-12 18:57

相关推荐

联系我们

在线咨询: QQ交谈

邮件:442814395@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信公众号