In-depth understanding of static and yield keywords in php

黄舟
Release: 2023-03-16 09:28:01
Original
1809 people have browsed it

This article mainly introduces you to the relevant information about the static and yield keywords in PHP. The article introduces it in great detail through sample codes. It has certain reference learning value for everyone to learn or use PHP. Friends who need this article Let’s learn with the editor below.

Preface

This article mainly introduces the relevant content about the static and yield keywords in PHP, and shares it for your reference and study. Not much to say below, let’s take a look at the detailed introduction.

Let’s talk about the static keyword first. This article only talks about the use of static methods and late binding.

#static When to use it to modify methods

#static keyword Everyone knows that it is used to modify methods and properties. So in what scenarios do you use it in your projects?

I have encountered several projects that require all methods to be static. Of course, controller methods cannot do this. One of the reasons is: static method execution efficiency is high? So let's analyze it based on this.

First of all, I have no objection to the high execution efficiency. Is it because it is highly efficient that it should be used in projects without restraint? To discuss this issue, let’s first review the history of programming languages. In the early days, there was no object-oriented programming, and structured programming was used. At that time, basically all methods were static methods. Then object-oriented came into being, and the concept of instantiation came into being.

It can be seen from the brief development process above that if it is just for performance, there seems to be no need for object-oriented existence. So what do these masters think about introducing object-oriented and instantiation into languages such as c++ and java? I think it's because with development, projects are getting bigger and bigger, requiring better organization of code and programming thinking.

Looking back at static, the static method it defines is indeed very efficient, but it will continue to occupy memory. The life cycle will only end when the program exits. One of the side effects is that it cannot be destroyed during the period; Secondly, from the design mode point of view, it has strong coupling, and the static attribute can be modified externally; thirdly, there is no way to override the method defined by static, and concepts such as ioc di are useless; fourthly, when performing unit testing, static methods Gives people a headache.

So based on the above, I feel that it is better not to use static methods in the future, but to instantiate and call methods honestly? We must be rational, and we cannot be so extreme that we use it everywhere, nor can we use it at all. Bottom line: Learn to think in an object-oriented way. I think the first consideration when we write code is: scalability (to cope with rapid business changes) and maintainability (to repair online problems in a timely manner). High efficiency should be considered last (because there are so many ways to optimize efficiency, it is not necessary to add: static to every method). If from an object-oriented perspective, this method is completely independent and has nothing to do with class attributes, then use static.

In short, we consider the use of syntax from an object-oriented perspective and the level of software design, rather than destroying the beauty of the code for efficiency.

static Late static binding

The PHP documentation introduces this in detail, but I have rarely paid attention to it before. This place basically uses self:: to call static methods and properties.

I think late binding is like overloading of static methods to some extent. Here is an example from the php document to describe it



        
Copy after login

Ifself::who()is called, it will output: A. If it isstatic::who(), it will output B

. From this point of view, is it equivalent to class B overriding thewho()method of parent class A? So if you use this feature flexibly, you can make static more flexible. Give full play to its performance advantages and solve the problem of poor scalability. Of course, it's still the same. From an object-oriented perspective, everything is done in moderation.

Usage scenarios of yield in PHP

To be honest, I didn’t know that PHP had such a syntax for a long time . Until one day I encountered this keyword in js. I felt that it was such an unclear thing. Why is it not available in the best language in the world? Looking back at the documentation, I can see that it is indeed the best language in the world.

So what are the usage scenarios of yield? Someone on sg just recently asked me to sort it out. I hope everyone can use it more in conjunction with their own business. There is no comparison between yield and Iterator. I believe that after reading it, you will be able to understand which of the two is more brief.

先说它的使用场景,还是得先回顾历史,在没有 yield 之前,我们要生成一个数组,只能一次性把所有内容全部读入内存(当然也可以通过实现 Iterator接口实现一个迭代)。有了 yield 之后,我们可以通过一个简单的 yield 关键字,完成一个数组的生成,并且是用到的时候才会产生值,相对而言内存占用肯定会下降。空口无凭,咱们下面通过代码实际检验一下上面的结论。

先来看普通模式



        
Copy after login

运行得到结果:


开始前内存占用:231528 生成完数组后内存占用:231712 释放后的内存占用:231576
Copy after login

前后的差值是:184

使用yield后的效果


function generateData($max) { for ($i = 0; $i <= $max; $i++) { yield $i; } } echo '开始前内存占用:' . memory_get_usage() . PHP_EOL; $data = generateData(100000);// 这里实际上得到的是一个迭代器 echo '生成完数组后内存占用:' . memory_get_usage() . PHP_EOL; unset($data); echo '释放后的内存占用:' . memory_get_usage() . PHP_EOL;
Copy after login

运行结果:


开始前内存占用:228968 生成完数组后内存占用:229824 释放后的内存占用:229016
Copy after login
Copy after login

前后的差值是:856

奇怪,使用了 yield 后,内存占用反而上升了,这是什么鬼?别急。上面我们参数传入的是 1,000,00,我现在将传入参数改成改成 1,000,000试试。

第一个方法得到的结果是:


开始前内存占用:231528 Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in /test/yield.php on line 6
Copy after login

看了吧,一百万次的循环时,一次性载入内存,超出了限制。那么再来看 yield 的执行结果:


开始前内存占用:228968 生成完数组后内存占用:229824 释放后的内存占用:229016
Copy after login
Copy after login

前后的差值依然是:856

好了到这里,应该看出来了,yield无论数组大小,占用均是 856 ,这是因为它自身,它在你进行迭代的时候才会产生真实数据。

所以如果你的数据来源非常大,那么用 yield 吧。如果数据来源很小,当然选择一次载入内存。

总结

The above is the detailed content of In-depth understanding of static and yield keywords in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!