as everyone knows Laravel For dependency injection, we only write how to use , I believe most people are not very clear about the principle of his implementation . Although you don't need to care about her principle in the process of using it , But understanding the principles makes you more confident to use them . This post goes through a little demo Let's briefly describe the implementation principle ,demo as follows , The demo Direct operation :

<?php
namespace Database;
use ReflectionMethod; class Database
{ protected $adapter; public function __construct ()
{} public function test (MysqlAdapter $adapter)
{
$adapter->test();
}
} class MysqlAdapter
{ public function test ()
{
echo "i am MysqlAdapter test";
}
} class app
{ public static function run ($instance, $method)
{
if (! method_exists($instance, $method)) return null; $reflector = new ReflectionMethod($instance, $method); $parameters = [
1
];
foreach ($reflector->getParameters() as $key => $parameter)
{ $class = $parameter->getClass(); if ($class)
{
array_splice($parameters, $key, 0, [
new $class->name()
]);
}
}
call_user_func_array([
$instance,
$method
], $parameters);
}
} app::run(new Database(), 'test');

The principle mainly uses PHP Reflection api Of  ReflectionMethod  class , stay PHP In operation , Extended analysis PHP Program . Please refer to the manual for specific use .

Laravel More articles on the principle of dependency injection

  1. php+laravel Dependency injection analysis

    laravel The container contains inversion of control and dependency injection , When it's used, it's , First put the object bind good , You can use it directly when you need it make Just come and get it . Usually our call is as follows . $config = $container->make('c ...

  2. laravel Dependency injection analysis

    laravel The container contains inversion of control and dependency injection , When it's used, it's , First put the object bind good , You can use it directly when you need it make Just come and get it . Usually our call is as follows . $config = $container->make('c ...

  3. PHP Dependency injection principle and usage analysis

    https://www.jb51.net/article/146025.htm This article gives an example of PHP Principle and usage of dependency injection . Share with you for your reference , As follows : introduction It's still an interview question from Darla , You know what? ...

  4. Spring Analysis of the principle of dependency injection

    Before analyzing the principle, let's review the concept of dependency injection : We often talk about dependency injection (Dependency Injection) And inversion of control (Inversion of Control) It's the same concept . The specific meaning is : Be a character ( ...

  5. 【 A string of lines 】spring The principle of generic dependency injection

    spring The principle of generic dependency injection It doesn't matter servlet : annotations @servlet service: annotations @service dao: annotations @Repository This is equivalent to registering these classes in the container

  6. 30 Lines of code let you understand angular Dependency injection :angular The principle of dependency injection

    Dependency injection (Dependency Injection, abbreviation DI) It's like C#,java It is a typical way to realize the control inversion of the design principles of the typical object-oriented language framework ,angular Introduce it to js in , Introduce angular rely on ...

  7. angular The principle of dependency injection

    Dependency injection (Dependency Injection, abbreviation DI) It's like C#,java It is a typical way to realize the control inversion of the design principles of the typical object-oriented language framework ,angular Introduce it to js in , Introduce angular rely on ...

  8. laravel Dependency injection Containers

    [ After reading, I can understand ]Laravel Service container ,IoC,DI      DI DI It's called dependency injection , So what is dependency injection ? For example , The computer ( It's not a notebook ) It takes a keyboard and a mouse for us to operate , This ‘ need ’ let me put it another way ...

  9. Spring Source code analysis bean The principle of dependency injection ( Injection properties )

          Recent research Spring bean Life cycle knowledge and source code , So I'm going to write an article Spring bean Life cycle related articles , But in the process of sorting out, we found that there are too many and complex points involved , It's hard to put... In an article Spri ...

Random recommendation

  1. Use c/c++ Expand python

    use python It's convenient to write scripts , But sometimes it needs to be expanded for a variety of reasons python( For example, providing programs with python Interfaces, etc. ). I always wanted to sort it out , Today, I finally sat down and did it , Record here , It's also convenient for me to check later . explain ...

  2. Reference resources __Linux

    course billie66.github.iocentos The configuration vsftpd Virtual user tutorial Linux The command of ubuntu14.04 To configure vsftp Practical skills Move Ubuntu16.04 The launcher on the left side of the desktop goes to ...

  3. Newton's method (Newton-Raphson Method)

    This blog has moved to http://www.kemaswill.com/, Blog park will continue to update , Welcome to your attention ~ Newton's method is a very effective numerical analysis method for solving equations . 1.   Newton's method hypothesis \(x_0\) It's an equation ...

  4. iOS block Different formats of declaration time and definition time

    When writing the program today , In the realization of a block Always prompt format error , contrast api Of block Parameter format , No mistakes were found . Later, I looked up the information , We find that the two formats are different ! The specific format is shown below NSString * (^testBlock) ...

  5. C# How to request data in

    #region according to URL Get the result set         /// <summary>        /// according to URL Get the result set The default is GET, If the amount of data is large, it can be passed in POST        // ...

  6. after I see Little Dorrit

    Maybe I'm too after fame and fortune , So I won't admit that I spent a lot of time watching movies , By blogging, I feel as if I have learned something from it , It seems that watching movies is also a way of learning . Maybe it's my way of calming down , I'm too busy playing or studying , Not even a chance to be very quiet One ...

  7. PMP The passing and passing of the exam is just

    I took part in it three times in more than a year PMP The test , The first two failed , It didn't work until the third time . How to deal with failure ? It's a challenge for everyone . Suppose we can treat temporary failures with the right attitude , Then the final success is not far away . I hope to share with you a ...

  8. Python Study _12_ Method and class customization

    Method In the last essay , Some methods of the class are mentioned briefly :__init__() And so on , And briefly explain the difference between method and function . Methods are functions defined inside a class , Methods are also objects , So methods are properties of classes , That's why instance methods exist in class definition ...

  9. Spark RDD operation

    1. Spark RDD Create operation 1.1 Data set   parallelize  Can create a parallel operation of RDD. Its function is defined as follows : ) scala> sc.defaultParallelism ...

  10. airbnb Open source reAir Tools Usage and source code analysis ( One )

    reAir There are batch copy and incremental copy functions Today, let's take a look at the bulk copy function Batch copy usage : cd reair ./gradlew shadowjar -p main -x test # If it's local tabl ...