依赖注入

2020-12-03 14:18:01 阅读:2 编辑
<?php

interface NewCache{
    public function output();
}
class Cache1 implements NewCache{
    public function output()
    {
        echo "cache1 output";
    }
}
class Cache2 implements NewCache{
    public function output()
    {
        echo "cache2 output";
    }
}
class Foo {
    private $cache;
    public function __construct(NewCache $cache)
    {
        $this->cache = $cache;
    }
    public function fun()
    {
        return $this->cache->output();
    }
}
Artisan::command('test', function () {
    app()->bind('NewCache','Cache1');
    app()->bind('foo','Foo');
    app('foo')->fun();
})->describe('test');