"How to introduce the concept of data injection variables in Java in PHP ?』 Segmentfault. comq1000000001. I don't know about this concept and its application scenarios. Can I introduce it? "How to introduce the concept of data injection variables in Java in PHP ?』
Http://segmentfault.com/q/10100000001...
I don't know about this concept and its application scenarios. Can I introduce it?
Reply content:
"How to introduce the concept of data injection variables in Java in PHP ?』
Http://segmentfault.com/q/10100000001...
I don't know about this concept and its application scenarios. Can I introduce it?
Instead of injecting variables, it is more appropriate to add referenced variables, as I answered in that question. The application scenario of this method is where all scenarios need to be referenced in the singleton mode. For example, if you call the Model in the Controller, you may need to write it like this.
class Controller_Test extends Controller{ public function execute() { $this->loadModel('user')->fetch(...); }}
This is called a call. No matter how you do the singleton mode, this object is called from the outside. However, if you follow the method I mentioned in the previous answer__get
The object called from the outside is injected into the internal attribute, and its advantages are obvious. In terms of writing, this reduces a lot of trouble and makes it clearer, in terms of performance, detection and function call consumption are reduced for multiple calls.
class Controller_Test extends Controller{ public function execute() { $this->userModel->fetch(...); }}
For example.
User:
id | name | age
In this way, if a variable is injected, an object is initialized.
$ User = new UserObj (); // at this time, the $ user instance does not load the name data echo $ user-> name; // inject only when the name attribute is called, load data
In fact, this UserObj does not define the name attribute, but is injected
Add a field birthday to the User.
id | name | age | birthday
The UserObj definition does not need to be modified.
$user = new UserObj();echo $user->birthday;