What is a data injection variable? What is its application scenario?

Source: Internet
Author: User
"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__getThe 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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.