Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the new features of PHP5.5

Shulou Source: shulou.com Published: 2022-06-03 06:43:11 11月15日 Update

This article will explain in detail what the new features of PHP5.5 are, and the editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

1 Generator yield keyword

The Chinese document of yield is here: http://php.net/manual/zh/language.generators.overview.php

If you look at the documentation, you can see that one of the functions of yield is that it can effectively reduce the memory overhead of iterations. For example, the xrange example on the official website:

The code is as follows:

Xrange here is an iteration, and the function is the same as range. If you use the range function, then the internal implementation of the function will store the intermediate process of each iteration, that is, each intermediate variable has a memory space, then the memory space used by the program is larger in the first place, and allocating memory and recycling memory will lead to longer running time of the program. But if you use the xrange function implemented on yield, all the intermediate variables in it use only one memory $I, which saves less time and space.

So why does yield have this effect? Think of yield in lua, this is the concept of collaborative process. In the Lua language, when the program runs to yield, the context is recorded by the co-program, and then the operation right of the program is returned to the main function. When the main function calls resume, the co-program is recalled and the context of the yield record is read. This forms the multi-program operation at the program language level. The same is true for yield in php 5.5. when the program runs to yield, the current program evokes the co-program logging context, and then the main function continues to operate, except that the php does not use keywords such as resume, but "call" the co-program when it is in use. For example, the foreach iterator in the above example can evoke yield. So the above example is understandable.

In fact, according to the reference yield, many internal functions, especially those related to iterations, should be possible to optimize. Maybe there will be yield versions and non-yield versions of functions that implement the same function.

2 finally keyword

This is the same as the finally in java, the classic try. Catch... Finally three-stage exception handling.

3 foreach supports list ()

To iterate over an array of arrays, you used to use two foreach, now you only need to use foreach + list, but the number of each array in the array of this array needs to be the same. The example of looking at the document can be understood at a glance.

The copy code is as follows:

4 empty () supports custom functions

Previously, parameters in empty () could not be functions. It's okay now.

The copy code is as follows:

6 class names can be obtained through:: class

The copy code is as follows:

7 added opcache extension

Using opcache will improve the performance of php, and you can add this optimization to static compilation (--enable-opcache) or dynamic extension (zend_extension) like other extensions.

This is the end of the article on "what are the new features of PHP5.5". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Function code program memory iteration array time up and down context example key keyword function variable document space article support run feature Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS NVidia Docker MySQL