Laravel 5.8 bottom learning note 01 - control inversion and dependency injection

Keywords: PHP Laravel

  1. home page
  2. special column
  3. PHP Garden
  4. Article details
0

Laravel 5.8 bottom learning note 01 - control inversion and dependency injection

IT pony Published 10 minutes ago

reference material:
Explanation of core concepts of laravel underlying core code analysis
PHP inversion of control (IOC) and dependency injection (DI)

1 - Laravel framework core

advantage

  1. Integrated composer
  2. Dependency injection is implemented to better manage class dependencies and facilitate extension (compared with MVC mode)
  3. Advanced features: console, event, queue, middleware, facade mode
  4. Core concept: service container serviceProvider

Disadvantages and optimization

shortcoming

  1. Too many files loaded, slow access

optimization

  1. Cache profile
  2. Remove unnecessary loading files (mainly serviceProvider)
  3. Open Opcache

Framework initiation process (life cycle)

  1. Reference autoload file
  2. Build service container
    1) Register basic bindings
    2) Register service container, event, routing and log services through bind
    3) Bind interface through bind
  3. Get Request object
  4. Logical processing
    1) Resolve startup items (basic services), such as routing, exception handling, facade and service container
    2) Through the pipeline mode, middleware is used to filter user requests and process business logic
  5. Return Response object

Knowledge points

Singleton mode, observer mode, pipeline mode
Dependency injection, anonymous function, reflection
Predefined interface ArrayAccess

2 - control inversion and dependency injection

inversion of control (IOC)

The dependencies between components are managed from the inside to the outside of the program

Explanation: instead of directly creating class B instances in class A, class B instances are transferred to class a through the IOC container

dependency injection (DI)

Inject component dependencies through external parameters or other forms

Example:

class DbMysql
{
    public function query(){}
}
class IOC
{
    public $db;
    public function __construct($dbMysql)
    {
        $this->db = $dbMysql;
    }
    public function action()
    {
        $this->db->query();
    }
}
$db = new DbMysql();
$c = new IOC($db);
$c->action();

The IOC class does not need to instantiate DbMysql, but passes in the instance of DbMysql as a parameter and only calls the method of DbMysql. This pattern is called dependency injection.

Putting the instantiation action of class B outside the IOC class is called control inversion.

Reflection mechanism of PHP

In PHP runtime, extend the analyzer to export or propose detailed information about classes, methods, properties and parameters. This function of dynamically obtaining and calling information is called reflection API.

class A 
{
    public function __construct(B $b)
    {
    }
}
class B 
{
}
//Get the reflection information of the class (all information)
$reflector = new ReflectionClass('A');
//Get constructor
$constructor = $reflector->getConstructor();
//Get constructor parameters
$dependencies = $constructor->getParameters();
//Gets the dependent class name
foreach ($dependencies as $dependency){
    if(!is_null($dependency->getClass())){
        $classname = $dependency->getClass()->name;
        $p[] = new $classname();
    }
}
//Create a new class instance from the given parameters
$a = $reflector->newInstanceArgs($p);

If class B also has dependent classes, it needs to be created recursively

<?php 
class A
{
    public function __construct(B $b)
    {
        $this->b = $b;
    }
    public function getB()
    {
        $this->b->bMethod();
    }
}
class B
{
    public function __construct(C $c,D $d)
    {
        $this->c = $c;
        $this->d = $d;
    }
    public  function bMethod()
    {
        echo "I am B Methods in bMethod()";
    }
}

class C{
    public function __construct()
    {
    }
    public function cMethod(){
        echo "I am C Methods in cMethod()";
    }
}

class D{
    public function __construct()
    {
    }
    public function dMethod(){
        echo "I am D Methods in dMethod()";
    }
}
class Ioc
{
    protected $instances = [];
    public function __construct()
    {
    }
    public function getInstance($classname){
        $reflector = new ReflectionClass($classname);
        $constructor = $reflector->getConstructor();
        $dependencies = $constructor->getParameters();
        if(!$dependencies){
            return new $classname();
        }
        foreach ($dependencies as $dependency){
            if(!is_null($dependency->getClass())){
                $instances[] = $this->make($dependency->getClass()->name);
            }
        }
        return $reflector->newInstanceArgs($instances);
    }
    public function make($classname){
        return $this->getInstance($classname);
    }
}
$ioc = new Ioc();
$a = $ioc->make('A');
$a->getB();

summary

The essence of PHP program running: including files and obtaining instantiated objects.
Traditional framework: manage class dependencies through include/require.
Laravel: through namespace and use, it realizes the automatic loading mechanism, finds the file where the class is located, and then obtains the instantiated object of the class through reflection.

Reading 13 was published 10 minutes ago
Like collection
PHP Garden
PHP rookie learning blog
Focus column

Version control: git

874 prestige
13 fans
Focus on the author
Submit comments
You know what?

Register login

Version control: git

874 prestige
13 fans
Focus on the author
Article catalog
follow
Billboard

Posted by Leviathan on Sat, 30 Oct 2021 00:58:45 -0700