


Detailed explanation of automatic dependency injection using reflection mechanism in PHP
This article describes how PHP implements the reflection mechanism and automatically Dependency Injection. For students who don’t know much about PHP reflection mechanism and automatic dependency injection, let’s take a look at this article!
Dependency injection is also called inversion of control. Anyone who has used the framework should be familiar with it. Many people think it is something very high-end when they see the name, and are daunted by it. Today I took the time to do some research and unravel its mystery. Without further ado, let’s go straight to the code;
/** * * 工具类,使用该类来实现自动依赖注入。 * */ class Ioc { // 获得类的对象实例 public static function getInstance($className) { $paramArr = self::getMethodParams($className); return (new ReflectionClass($className))->newInstanceArgs($paramArr); } /** * 执行类的方法 * @param [type] $className [类名] * @param [type] $methodName [方法名称] * @param [type] $params [额外的参数] * @return [type] [description] */ public static function make($className, $methodName, $params = []) { // 获取类的实例 $instance = self::getInstance($className); // 获取该方法所需要依赖注入的参数 $paramArr = self::getMethodParams($className, $methodName); return $instance->{$methodName}(...array_merge($paramArr, $params)); } /** * 获得类的方法参数,只获得有类型的参数 * @param [type] $className [description] * @param [type] $methodsName [description] * @return [type] [description] */ protected static function getMethodParams($className, $methodsName = 'construct') { // 通过反射获得该类 $class = new ReflectionClass($className); $paramArr = []; // 记录参数,和参数类型 // 判断该类是否有构造函数 if ($class->hasMethod($methodsName)) { // 获得构造函数 $construct = $class->getMethod($methodsName); // 判断构造函数是否有参数 $params = $construct->getParameters(); if (count($params) > 0) { // 判断参数类型 foreach ($params as $key => $param) { if ($paramClass = $param->getClass()) { // 获得参数类型名称 $paramClassName = $paramClass->getName(); // 获得参数类型 $args = self::getMethodParams($paramClassName); $paramArr[] = (new ReflectionClass($paramClass->getName()))->newInstanceArgs($args); } } } } return $paramArr; } }
The above code uses PHP’s reflection function to create a container class, and uses this class to implement the dependency injection function of other classes. The above dependency injection is divided into two types, one is dependency injection of constructor, and the other is dependency injection of method. We use the following three classes for testing.
class A { protected $cObj; /** * 用于测试多级依赖注入 B依赖A,A依赖C * @param C $c [description] */ public function construct(C $c) { $this->cObj = $c; } public function aa() { echo 'this is A->test'; } public function aac() { $this->cObj->cc(); } } class B { protected $aObj; /** * 测试构造函数依赖注入 * @param A $a [使用引来注入A] */ public function construct(A $a) { $this->aObj = $a; } /** * [测试方法调用依赖注入] * @param C $c [依赖注入C] * @param string $b [这个是自己手动填写的参数] * @return [type] [description] */ public function bb(C $c, $b) { $c->cc(); echo "\r\n"; echo 'params:' . $b; } /** * 验证依赖注入是否成功 * @return [type] [description] */ public function bbb() { $this->aObj->aac(); } } class C { public function cc() { echo 'this is C->cc'; } }
1. Dependency injection of test constructor
// 使用Ioc来创建B类的实例,B的构造函数依赖A类,A的构造函数依赖C类。 $bObj = Ioc::getInstance('B'); $bObj->bbb(); // 输出:this is C->cc , 说明依赖注入成功。 // 打印$bObj var_dump($bObj); // 打印结果,可以看出B中有A实例,A中有C实例,说明依赖注入成功。 object(B)#3 (1) { ["aObj":protected]=> object(A)#7 (1) { ["cObj":protected]=> object(C)#10 (0) { } } }
2. Dependency injection of test method
Ioc::make('B', 'bb', ['this is param b']); // 输出结果,可以看出依赖注入成功。 this is C->cc params:this is param b
It can be seen from the above two examples that we Create objectsOr when calling a method, there is no need to know which class the class or the method depends on. Using the reflection mechanism can easily automatically inject the required classes for us.
Summary
Okay, do you think the above code is very simple? In fact, as long as you are familiar with the reflection mechanism of PHP, Dependency injection is not difficult to implement. The above code is written simply to facilitate understanding. In actual projects, it will definitely not be so simple. For example, the injected classes and parameters will be configured, and the instantiated instances will be cached. Class, the next time you need an instance of this class, you can use it directly without re-initializing it, etc. But I believe that once you understand the principles, you can improve the rest according to the needs of the project.
The above is the detailed content of Detailed explanation of automatic dependency injection using reflection mechanism in PHP. For more information, please follow other related articles on the PHP Chinese website!

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
