Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use reflection to realize dependency injection in laravel

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In order to solve the problem of how to use reflection to achieve dependency injection in laravel, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Dependency injection

You often rely on other objects in a class, so take a look at the classic way to write it.

Class Foo {public $bar; public function _ construct () {$this- > bar = new Bar ();}} $foo = new Foo ()

When the dependency of a class changes, for example, when the class Bar needs to instantiate parameters, and there are many classes that depend on it, you can't modify them one by one.

Take another look at what you can do with dependency injection

Class Foo {public $bar; public function _ construct ($bar) {$this- > bar = $bar;}} $bar = new Bar (); / / $bar = new Bar ($args); $foo = $foo = new Foo ($bar)

After the Bar class is instantiated externally, it is passed into the Foo class as a parameter, thus realizing the control inversion. If the Bar class needs parameters now, the external modification is fine, and there is no need to modify the classes that depend on it one by one.

Dependency injection in laravel

In laravel, you often write the following code

Class SomeController {public function index (Request $request) {dd ($request- > all ());}}

As long as you declare Request $request in the method parameters, you can use the $request object directly, which is very convenient.

In fact, laravel uses PHP's reflection mechanism to do $request = new Request for us behind the scenes. Reflection is the reflective ability of a class to export the details of the class, including properties, methods, even comments, and so on. For reflection, you can view the PHP document http://php.net/manual/zh/intro.reflection.php

Implementation, look at the code

$method = new ReflectionMethod ('SomeController',' index'); $args = []; foreach ($method- > getParameters () as $parameter) {if ($class = $parameter- > getClass ()) {$args [] = new $class- > name; / / $request = new Request}} $method- > invokeArgs (new SomeController, $args)

Get the parameters of the class method through ReflectionMethod. If the parameters are other classes, they are instantiated and passed into the class method using ReflectionMethod::invokeArgs as parameters. The principle is that simple.

New ReflectionClass ('className') is usually used to reflect classes and ReflectionMethod to reflect class methods.

This is the answer to the question about how to use reflection to achieve dependency injection in laravel. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report