php simple understanding of dependency injection and automatic loading

Reference: https://blog.csdn.net/soonfly/article/details/52627683

Reference: https://blog.csdn.net/sd19871122/article/details/78696114

       Because I was learning the tp5 framework, I often saw a way of writing __construct(Request $request){$this->request=$request}. I don’t know what it means. Later, Baidu took a look. This is called dependency injection. I have looked for a lot of articles to see the meaning of, and I have some relatively shallow understanding, so I will record it here.

       First of all, what is a dependency? A dependency is that a class A refers to a class B and instantiated it in a function. If there is no class B, class A will report an error and fail to execute. So there is a dependency between A and B, and A depends on B.

       Knowing what a dependency is, then continue to talk about dependency injection. Simply put, dependency injection is not to instantiate B in class A, but to instantiate class B outside of class A, and then pass in class A through parameters, and then assign value to class A in class A Custom variables in the class. Give (fu) a (zhi) chestnut (zhan) son (tie):

Since the boss didn’t write everything, so I’ll add something

class Storage{

      function   __construct($text=''){

           echo $text;

       }

}

  • Constructor injection
class User
{
  function __construct($storage)
  {
    $this->storage = $storage;
  }

  // ...
}

new  $user=User(new Storage('text'));

  • setter method injection
class User
{
  function setSessionStorage($storage)
  {
    $this->storage = $storage;
  }

  // ...
}

new  $user=User();

$user->setSessionStorage(new Storage('text'))

  • Attribute direct injection
class User
{
  public $sessionStorage;
}

$user->sessionStorage = $storage;

$user->sessionStorage=new Storage('text'))

According to experience, components with strong dependencies are generally injected through the constructor, and the setter method is used to inject optional dependent components. 

       Everyone feels that the advantage of dependency injection is that there is no need to repeatedly modify the code. For example, your class B is outdated and needs to be replaced with class C, but if the method name remains the same, if you don’t need dependency injection, you have to go to class A and find class newB In the place, change it to the new C category. Dependency injection only needs to change type B to type C when it is used, saving the process of finding.

       Okay, let's talk about autoloading after dependency injection. Automatic loading is very useful, it will be triggered when you call a class that does not exist, the mechanism is to trigger before the error is reported. There are two ways to trigger automatic loading __autoload and spl_autoload_register(). for example:

       

<?php
function aaa($class){
    echo $class;
}
function bbb(){
    echo 123;
}

spl_autoload_register('aaa');
spl_autoload_register('bbb',true,true);
$a=new aaa();

Output:

123aaa
Fatal error: Class 'aaa' not found in E:\phpstudy\PHPTutorial\WWW\index.php on line 11

        spl_autoload_register() is used to register functions that are triggered when a class that does not exist is called. The order of triggering is in accordance with the order of registration. There are two more parameters after spl_autoload_register(), spl_autoload_register('bbb',true,true); the first true is whether spl_autoload_register() throws an exception when the function written in spl_autoload_register() fails to register successfully  . The second true will put the registered function in the first run, and will not be triggered in the order of registration when it is triggered. The function of __autoload is the  same as spl_autoload_register(), but __autoload can only register one function.

        The above is my personal understanding of dependency injection and automatic loading. If there is an error, please give me advice.

Guess you like

Origin blog.csdn.net/weixin_42094764/article/details/83416783