当前位置:网站首页>Understand (DI) dependency injection in PHP

Understand (DI) dependency injection in PHP

2022-06-25 03:51:00 InfoQ


Preface

A lot of people are learning php The term dependency injection will be heard after a period of time , But I don't quite understand , I understand that dependency injection is actually a kind of php Programming design pattern , Although it has not been placed in the design pattern , Design patterns exist for the efficiency of programming , Dependency injection is, of course .

One 、 What is dependency injection  (DI)

Dependency injection  (DI) In fact, it essentially means that the dependency on the class is automatically injected through the constructor

Generally speaking , Is that you currently operate on a class , However, some methods or functions of this class can not be completed by this class alone , But with the help of another class

The most direct sign is when the parameter data is transferred as an object . Strictly speaking , You want to manipulate another class in another class , The two classes are interdependent , The way to pass parameters is called injection

Two 、 Why dependency injection occurs

At the beginning ,php When you need to use another class in one class , Usually   Will perform the following operations :

For example, I am in container Class adapter class , You need to instantiate before using

If you need to use a large number of external classes , This will result in too high coupling , It is easy to cause difficulties in later maintenance

In layman's terms , That is to say container Can't work without external classes , This is called too high coupling

class` `container``{`` ``private` `$adapter``;` ` ``public` `function` `__construct()`` ``{`` ``$this``->adapter = ``new` `adapter();`` ``}`` ``}

3、 ... and 、 Simple dependency injection

The code coupling above is too high , Led to the emergence of dependency injection , Mainly for understanding coupling

as follows , We just need to pass in the class object we need to operate

The parameter of dependency injection operation is object , Instead of ordinary parameters , Is there a better understanding

But such simple dependency injection , If you depend on many classes , It will be a long time for you to pass , It's easy to get confused

class` `container``{`` ``private` `$adapter``;` ` ``public` `function` `__construct(adapter ``$adapter``)`` ``{`` ``$this``->adapter = ``$adapter``;`` ``}``}

Four 、 Higher order dependency injection

In order to solve the problem of parameter confusion above , Now , Dependency injection evolved

By magic ,__get To set the object

Now , We can solve the problem of relying too much , The problem of parameter confusion

class` `container``{`` ``public` `$instance` `= [];`` ` ` ``public` `function` `__set(``$name``, ``$value``)`` ``{`` ``$this``->instance[``$name``] = ``$value``;`` ``}``}`` ` `$container` `= ``new` `container();` `$container``->adapter = ``new` `adapter();

5、 ... and 、 Application of dependency injection

Let's first define a container class , It is mainly used to inject the class you want to operate into the container

When you use it , Just pass the container as an object

class` `container``{`` ``public` `$instance` `= [];`` ` ` ``public` `function` `__set(``$name``, ``$value``)`` ``{`` ``$this``->instance[``$name``] = ``$value``;`` ``}``}` `class` `adapter``{`` ``public` `$name` `= ``' I'm the scheduler '``;``}` `$container` `= ``new` `container();` `$container``->adapter = ``new` `adapter();` ` ` `class` `autofelix``{`` ``private` `$container``;`` ` ` ``public` `function` `__construct(container ``$container``)`` ``{`` ``$this``->container = ``$container``;`` ``}`` ` ` ``public` `function` `who(``$class``)`` ``{`` ``return` `$this``->container->instance[``$class``]->name;`` ``}``}`` ` `$autofelix` `= ``new` `autofelix(``$container``);`` ` `$who` `= ``$autofelix``->who(``'adapter'``);`` ` `var_dump(``$who``); ``// I'm the scheduler

6、 ... and 、 Higher order optimization

In the application above , We directly inject the instantiated object into the container

And that leads to , All objects will be instantiated before they are used , Cause the loss of resources

We can pass in closures , In this way, the object will not be instantiated and injected , When you need to use it yourself , Then instantiate , You can reduce the loss of server resources

$container` `= ``new` `container();``$container``->adapter = ``new` `adapter();`` ` ` ` `// Higher order optimization ``$container` `= ``new` `container();``$container``->adapter = ``function` `() {`` ``return` `new` `adapter();``};

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250033007254.html