Design Patterns-how to introduce the concept of data injection variables in Java in PHP?

Source: Internet
Author: User
@ 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__getIn this magic method

class Base{    public __get($varName)    {        $className = ucfirst($varName);        $this->{$varName} = new $className;        return $this->{$varName};    }}

In this way, when I inherit fromBaseClass, such$this->userModel, It will call__getMethod, 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...

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.