Home > PHP Framework > Laravel > body text

What is dependency injection and inversion of control in laravel

WBOY
Release: 2022-06-06 11:39:38
Original
2306 people have browsed it

In laravel, dependency injection is a behavior of injecting components into an application, which is an explicit declaration of dependencies; inversion of control is a design principle of object-oriented programming, used to reduce the complexity of computer code. The degree of coupling between classes means that one class hands over its control to another object, and the dependencies between classes are resolved by this object.

What is dependency injection and inversion of control in laravel

#The operating environment of this article: Windows 10 system, Laravel version 6, Dell G3 computer.

What is laravel's dependency injection and control inversion

1. Dependency injection

The term dependency injection is a term proposed by Martin Fowler. It is the act of injecting components into an application. As Ward Cunningham said:

Dependency injection is a key element in agile architecture.

Example:

class UserProvider{
    protected $connection;
 
    public function __construct(){
        $this->connection = new Connection;
    }
 
    public function retrieveByCredentials( array $credentials ){
        $user = $this->connection
                        ->where( 'email', $credentials['email'])
                        ->where( 'password', $credentials['password'])
                        ->first();
 
        return $user;
    }
}
Copy after login

If you want to test or maintain this class, you must access the database instance to perform some queries. To avoid having to do this, you can decouple this class from other classes, you have one of three options to inject the Connection class without using it directly.

2. Inversion of Control

Inversion of Control (Inversion of Control, abbreviated as IoC) is a design principle in object-oriented programming that can be used Reduce coupling between computer codes. The most common method is called Dependency Injection (DI), and the other method is called Dependency Lookup. Through inversion of control, when an object is created, an external entity that controls all objects in the system passes the reference of the object it depends on to it.

To put it simply, a class hands over its control to another object, and the dependency between classes is resolved by this object. Dependency injection is an explicit declaration of dependencies, while dependency lookup resolves dependencies through lookup.

Inversion of control is not a method, but a design idea. Laravel is the same as the Spring framework. There is no need to worry about the inversion of the noun. The explanation is that traditional objects actively take the attributes assigned to the object. , and all we do in the framework is to operate the container. The container will automatically find the required attributes through mapping and assign them. The object only passively accepts dependent objects. Let’s look at the problem from the way of obtaining the object attributes.

Extended knowledge:

Usage in Laravel

Inject a class:

App::bind('foo', function($app){    return new FooBar;
});
Copy after login

This example means creating an alias For the class foo, FooBar is actually instantiated when used.

The method to use this class is:

$value = App::make('foo');

$value is actually a FooBar object.

If you want to use singleton mode to instantiate a class, then use:

App::singleton('foo', function(){    return new FooBar;
});
Copy after login

In this way, the same object will be instantiated every time.

[Related recommendations: laravel video tutorial]

The above is the detailed content of What is dependency injection and inversion of control in laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!