@ Joyqi mentioned in an interview with infoq: in terms of the framework, we introduced the concept of Java injection variables in PHP. Although they are not completely the same, they are also similar. The implementation is actually very simple. It is just a few tips for PHP, But it solves a headache in the PHP project... @ joyqi mentioned in the infoq interview:
In terms of the framework, we introduced the concept of injecting variables in Java in PHP. Although they are not completely the same, they are also similar. The implementation is actually very simple. It is just a few tips for PHP, But it solves a headache in the PHP project, that is, the free reference of modules.
@ Gaosboy mentioned later:
Multiple scenarios require different data models for different combinations, but we do not need to define data structures for these scenarios separately. Instead, we only need to call them directly during use, and the framework will inject data at runtime.
I didn't understand it. Could you share with me how to implement it? Thank you.
Reply content:
@ Joyqi mentioned in infoq's interview:
In terms of the framework, we introduced the concept of injecting variables in Java in PHP. Although they are not completely the same, they are also similar. The implementation is actually very simple. It is just a few tips for PHP, But it solves a headache in the PHP project, that is, the free reference of modules.
@ Gaosboy mentioned later:
Multiple scenarios require different data models for different combinations, but we do not need to define data structures for these scenarios separately. Instead, we only need to call them directly during use, and the framework will inject data at runtime.
I didn't understand it. Could you share with me how to implement it? Thank you.
Actually, it is not as complicated as you think. Zend Framework is famous for its complexity in simple things. In fact, the process of Variable Injection in the class can be summarized as follows:
class A{ private $myObject; public function setMyObject($myObject) { $this->myObject = $myObject; }}$a = new A();$a->setMyObject(new Object());
In a word, an external object is introduced as the internal attribute of the class, but I don't want to bend around so much during framework design. In java, the reflection mechanism is used to detect the object to be injected, so you have to write a bunch of set methods, Because java is resident memory, so it doesn't matter if it is slow for the first injection, but If php is so efficient, it will be too low. So I do the opposite. I don't need a class to declare what variables should be injected to the outside. I just need to know which variables should be used. The specific technique is__get
In this magic method
class Base{ public __get($varName) { $className = ucfirst($varName); $this->{$varName} = new $className; return $this->{$varName}; }}
In this way, when I inherit fromBase
Class, such$this->userModel
, It will call__get
Method, automatic Initializationnew UserModel()
And set the variable to the attributes of the current class. The second call does not have to be initialized.
This is just a specific idea. The specific matching and initialization rules will be more complex, and you may need to implement the singleton mode to avoid repeated initialization of the same object in different classes, but these are all very simple. The most important thing is this step of thinking. It is actually very easy to break it down. I believe many of my friends have already used it.
It should refer to dependency injection. The implementation of Zf2 is perfect, and almost all DI scenarios are taken into account.
Http://framework.zend.com/manual/2.0...
Source code:
Https://github.com/zendframework/zf2...