Simple interest mode and dependency injection, I believe many people are not unfamiliar. This paper mainly explains Di dependency injection and singleton mode of PHALAPI, and students who are interested in dependency injection and singleton mode can look at it.
1. Single-case mode
Singleton mode for the long-term object-oriented programming of children's shoes should not be unfamiliar, in the learning of PHP children's shoes should have heard, here simply talk about the single-case mode is a what kind of things, solve what problems, and in the Phalapi how to achieve.
Single case, the so-called Singleton is there and only one exists, this is a single case, it is not difficult to see that he benefits from the use of less resources because there is only one, we all know to use a class must be an instance he is new at each new object will be in memory to generate an area to hold this instance, If you use a lot of new instantiation of the same object in one run of the program, it consumes resources, but if it is a generic one that uses global variables, it will appear to be not elegant and messy, in which case the singleton pattern arises.
The singleton pattern is one of the two worlds of a method that can be used globally, without worrying about taking up too much resources, and three very elegantly, let's take a look at how the singleton pattern is implemented in PHALAPI:
You see that our common Di method is implemented internally by the static method in Phalapi_di One method function DI () { return Phalapi_di::one ();}
Then we look inside the one method
Whenever we ask to come over, verify that the static variable instance is not initialized, and if it is the first call, he will instantiate the Phalapi_di class internally and then return the negative value to $instance, then returning the instance to the static variable, The next time we ask for it, this static variable has been instantiated and will skip the instance directly to return to this object, that is, the Di method that we use in the PHALAPI framework is actually an object, there is only one area in memory, the code is as follows:
public static function one () { if (self:: $instance = = NULL) {self :: $instance = new Phalapi_di (); Self:: $instance->onconstruct (); } Return self:: $instance;}
It's not hard, we just use it. The operation of the new class is encapsulated into the static method of the class that we need to be new, the same as the above to judge, it can be very simple to implement the singleton mode.
2. Dependency Injection
Dependency injection, also known as "inversion of control," if it is familiar with the javaweb development of the spring framework should have a relatively deep feeling, here also not to deep talk, Just a brief explanation of the implementation of Di dependency injection in Phalapi gives you an idea of how this design pattern is implemented and the lazy loading mechanism implemented from there.
2.1 Di Dependency Injection implementation
The Di () method commonly used in Phalapi, that is, using the so-called Singleton pattern above, that is, every time we use Di () is actually using the Phalapi_di class, then we rely on the injection of the key is in the Phalapi_di
First of all, let's talk about one of his implementations in terms of specific implementations, here's an example:
Configure Di ()->config = new Phalapi_config_file (api_root. '/config ');
In fact there is an array inside, it is the Config as the key, bar new Phalapi_config_file (Api_root. '/config ') as value and then save it when we next use Di->config->get (), it will be based on the key value of the config to start the new class, so it can be said that the config operation is dependent on Di (), And the use of Di ()->config is always used in an instance, but also to reduce the consumption of resources.
Some children's shoes are curious why Di ()->config will be saved to the array need to be taken out, interested children's shoes can Baidu Magic method ser and get
/** You can see this is the Magic method in Phalapi_di __set * that is, when using Di ()->config = new Phalapi_config_file (api_root. '/config '); The name value obtained is the Config, and the obtained value is the new Phalapi_config_file (Api_root. '/config '); */get Similarly, internal implementations are called internal get and Set methods Public function __set ($name, $value) { $this->set ($name, $value);} Public Function __get ($name) { return $this->get ($name, NULL);}
After reading people do not feel very simple ah, we can also in their own design class when the use of this flexible magic method to achieve.
2.2 Lazy Loading
The Di () method in Phalapi also provides lazy loading, where lazy loading is not loaded when the class is not being used, and this is done to avoid wasting unnecessary resources. When we're not using it, we'll never instantiate it. Only when you use the new class and then instantiate it, let's see how it's done.
When we execute the following statement, the value that is stored in the dependency injection is the TESTDI ()->test = ' test ' of the string,//Using Di ()->test->test (); will use the Get method in Phalapi, there is a code in the Get method $this->data[$key] = $this->initservice ($this->data[$key]);// Inside the Initservice method, it is validated that value is a string that is instantiated and returned if ($config instanceof Closure) { $rs = $config ();} ElseIf (is_string ($config) && class_exists ($config)) { $rs = new $config (); if (is_callable (Array ($rs, ' oninitialize '))) { Call_user_func (array ($rs, ' oninitialize '));} } else{ $rs = $config;}
Related recommendations:
Phalapi How to realize the separation of database read and write
phalapi-Cache usage and Redis expansion
Phalapi (π frame)