This article mainly introduces to you about the static and yield keywords in PHP related Materials, the text through the sample code introduced in very detailed, for everyone to learn or use PHP has a certain reference learning value, the article needs friends below with the small series to learn together.
Objective
This article mainly introduces to you about the static and yield keywords in PHP related content, share out for everyone to reference the study, the following words do not say, come together to see the detailed introduction bar.
First, say the static keyword. This article only speaks of the use of static methods and late binding knowledge points.
When is static used to modify the method?
The static keyword is known to be used to modify methods and properties. So what scenarios will everyone use in the project?
I have encountered several projects that require all methods to be completely static, but the Controller method cannot do so. One reason: Static method execution is efficient? So we'll analyze it based on this.
First of all, I have no problem with the high efficiency. Is it because it is so efficient that it should be used in a project without restraint? Discuss this question first to review the history of the programming language. In the early days, there was no object-oriented, and all were structured programming, when basically all the methods were static methods, then there was object-oriented, resulting in the concept of instantiation.
From the brief development process above, it can be seen that, if only for performance, where object-oriented seems to be no need to exist. So what do these masters do in order to introduce object-oriented, instanced, and instantiated words into C + + Java? I think it is because of the development, the project is getting bigger, need better organization code way and programming thinking.
And then looking back at static, it defines statically, but it is very efficient, but it will continue to occupy memory, only when the program exits the end of the life cycle, the period can not be destroyed and other side effects are the first, and in design mode, it has strong coupling, external modifiable static properties ; its three-static definition method has no way to override, the concept of IOC di is useless, and its four in the unit test, the static method is a headache.
So, by the above, it feels like you're not going to use the static method, just instantiate and then invoke the method? We have to be rational, we can't use it in extreme places, and we can't do it at all. Bottom line: Learn to think in an object-oriented way. The first thing we think about when we write code is: scalability (for fast business Change), maintainability (online problem fix). High efficiency should be the last consideration (because there are so many ways to optimize efficiency, and not necessarily to add one to each method: static). If this method is completely independent of the class attribute from an object-oriented perspective, then use static.
In short, stand in the object-oriented perspective, the level of software design to consider the use of grammar, rather than for efficiency to destroy the beauty of code.
Static late binding
This is a little bit of PHP documentation, but I've been paying little attention to this place before, basically using the self:: method to invoke static methods and properties.
I think late binding to some extent, like the overloading of static methods. Here's an example of a PHP document to tell
<?phpclass A {public static function who () {echo __class__;} public static function test () {self::who (); STATIC::WH O ();//late static binding}}class B extends A {public static function who () {echo __class__;}} B::test ();
If it is self::who()
called, it outputs: A. If it static::who()
will output B
Does this mean that Class B overrides the parent Class A who()
method? If you use this feature flexibly, you can make the static more flexible. Give full play to its performance advantages, but also to solve the problem of poor scalability. Of course, the same, from an object-oriented perspective, everything is enough.
Usage scenarios for yield in PHP
To tell you the truth, for a long time I didn't know that PHP had such a grammar. Until one day I met this keyword in js, feeling so unknown to the things, the best language in the world is not? Looking back at the document, really, deserves to be the best language in the world.
So what is the usage scenario for yield? Just recently someone on the SG asked me to tidy it up. I hope you will be able to use it more in conjunction with your own business. There is no comparison between yield and Iterator. Believe that after reading, you can understand the two who more profile.
First of all, the use of the scene, or you have to review the history, before yield, we want to generate an array, can only be used to read all the content in memory (of course, can also be implemented by implementing the iterator interface to achieve an iteration). With yield, we can use a simple yield keyword to complete the generation of an array, and it is used to produce a value, relative memory consumption will certainly fall. Word ..., let's go through the code to actually test the above conclusions.
First look at normal mode
<?phpfunction Generatedata ($max) {$arr = []; for ($i = 0; $i <= $max; $i + +) {$arr [] = $i;}} Echo ' pre-start memory consumption: '. Memory_get_usage (). php_eol; $data = Generatedata (100000); Echo ' memory consumption after generating array: '. Memory_get_usage (). Php_eol;unset ($data); Echo ' memory consumption after release: '. Memory_get_usage (). Php_eol;
Run to get results:
Pre-start memory consumption: 231528 memory consumption after generating array: 231712 memory consumption after release: 231576
The difference between the front and back is: 184
Effects after use of yield
function Generatedata ($max) {for ($i = 0; $i <= $max; $i + +) {yield $i;}} Echo ' pre-start memory consumption: '. Memory_get_usage (). php_eol; $data = Generatedata (100000);//What is actually obtained here is an iterator echo ' memory footprint after generating the array: '. Memory_get_usage (). Php_eol;unset ($data); Echo ' memory consumption after release: '. Memory_get_usage (). Php_eol;
Operation Result:
Pre-start memory consumption: 228968 memory consumption after generating array: 229824 memory consumption after release: 229016
The difference between the front and back is: 856
Strangely, after using yield, memory usage rises instead, what is this ghost? Don't worry. Above our parameters are 1,000, 00, I now change the incoming parameter to 1,000,000 to try.
The results from the first method are:
Pre-start memory consumption: 231528Fatal error:allowed memory size of 134217728 bytes exhausted (tried to allocate) bytes P on line 6
Look, 1 million times the loop, once loaded into memory, beyond the limit. Then look at the results of yield execution:
Pre-start memory consumption: 228968 memory consumption after generating array: 229824 memory consumption after release: 229016
The difference between the front and back is still: 856
Well, here, it should be seen, yield regardless of the array size, the occupancy is 856, because it itself, it will be in your iteration when the real data is generated.
So if your data source is very large, then use yield. If the data source is small, of course you choose to load the memory once.
Summarize