Home  >  Article  >  PHP Framework  >  What is ioc control inversion in laravel

What is ioc control inversion in laravel

WBOY
WBOYOriginal
2022-03-15 11:22:082747browse

In laravel, IOC inversion of control is a design principle in object-oriented programming. It can be used to reduce the coupling between computer codes. It means that one class hands over its control to another. Object, the dependency between classes is resolved by this object.

What is ioc control inversion in laravel

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

What is ioc control inversion in laravel

Inversion of Control (Inversion of Control, abbreviated as IoC) is a design principle in object-oriented programming that can be used to reduce computer code degree of coupling between them.

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 (IoC): No need to modify the content yourself, instead it is passed externally. Change from internal dependencies to external dependencies.

Inversion Of Control (IOC): 'Control' is the control of the program execution flow. 'Inversion' means that before using the framework, the programmer controls the execution of the entire program. After using the framework, Control is transferred from the programmer to the framework.

Inject a class:

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

This example means to create a class with the alias foo, and the actual instantiation when using it is FooBar.

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;
});

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 ioc control inversion in laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:Why laravel uses queueNext article:Why laravel uses queue