Skip to content

JustPoet/SimDI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimDI

一个简单的PHP依赖注入框架,支持autowire。

附上之前写的一篇文: 用PHP撸一个DI容器

安装

composer require zean/sim-di

使用

  1. 不使用面向接口编程风格

    假如有Car和Driver两个类,Driver依赖Car

    class Car
    {
        protected $name = '汽车';
     
        public function getName()
        {
            return $this->name;
        }
    }
    class Driver
    {
        protected $car;
     
        public function __construct(Car $car)
        {
            $this->car = $car;
        }
     
        public function drive()
        {
            return '驾驶' . $this->car->getName();
        }
    }

    当我们需要Diver实例的时候,这时候我们只需要让容器创建,容器会自动注入Car实例

    $app = \SimDI\Container::getInstance();
    $driver = $app->get(Driver::class);
    echo $driver->drive();

output:驾驶汽车

  1. 使用面向接口编程风格

假如我们用面向接口的方式来,我稍微修改一下上面的代码:

abstract class Car
{
    protected $name = '汽车';
    
    public function getName()
    {
        return $this->name;
    }
}
interface Driveable
{
    public function run();
}
class Benz extends Car implements Driveable
{
    protected $name = '奔驰';
    
    public function run()
    {
        return $this->getName() . '启动了!';
    }
}
class Driver
{
    protected $car;
    
    public function __construct(Driveable $car)
    {
        $this->car = $car;
    }
    
    public function drive()
    {
        return '驾驶' . $this->car->run();
    }
}

面向接口编程时需要有一个配置来指定interface和实现类的对应关系,如下:

$config = [
    Driveable::class => Benz::class,
];

然后在创建容器的时候我们使用上述配置:

$app = \SimDI\Container::getInstance($config);
$driver = $app->get(Driver::class);
echo $driver->drive();

output:驾驶奔驰启动了!