Parsing PHP5.5 new features _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Parse PHP5.5 new features. PHP5.5 was just released recently. What are the new features in PHP5.5? Official documentation is here: www. php. netmanualzhmigration55.new-features. php1 generator yield keyword in PHP5.5 recently released. What are the new features in PHP5.5? The official documentation is here:
Http://www.php.net/manual/zh/migration55.new-features.php
1 generator yield keyword
Yield Chinese documentation here: http://php.net/manual/zh/language.generators.overview.php
You can see that yield can effectively reduce the memory overhead of iteration. For example, the xrange example on the official website:

The code is as follows:


Function xrange ($ start, $ limit, $ step = 1 ){
For ($ I = $ start; $ I <= $ limit; $ I ++ = $ step ){
Yield $ I;
}
}

Echo 'single digit odd numbers :';

/*
* Note that an array is never created or returned,
* Which saves memory.
*/
Foreach (xrange (1, 9, 2) as $ number ){
Echo "$ number ";
}

Echo "\ n ";
?>


Here, xrange is an iteration with the same functions as range. if the range function is used, the internal implementation of the function will store the intermediate process of each iteration, that is to say, every intermediate variable has a memory space. First, the memory space used by the program will be large, and memory allocation and memory recovery will lead to a longer running time of the program. However, if you use the xrange function implemented by yield, all the intermediate variables in the function only use one memory $ I, which reduces the time and space required.

Why is yield so effective? I think of yield in lua, which is the concept of coroutine. In lua, when the program runs to yield, the coroutine is used to record the context environment and return the program operation permission to the main function. when the main function calls resume, the coroutine is refreshed to read the context of the yield record. This forms a multi-coroutine operation at the language level. The same applies to yield in php 5.5. when the program runs to yield, the current program will invoke the context of the coroutine record, and then the main function will continue to operate, in php, the keyword like resume is not used, but the coroutine called during use. For example, the foreach iterator in the previous example can invoke yield. So the above example can be understood.

In fact, according to reference yield, a lot of internal functions, especially iteration-related functions, should be likely to be optimized. In the future, yield and non-yield versions may implement the same function.

2 finally keywords
Like finally in java, this is the classic try... catch... finally three-stage exception handling.

3 foreach supports list ()
For the iteration of "array", we need to use two foreach before. now we only need to use foreach + list, but the number of each array in this array needs to be the same. You can see the example in the document at a glance.

The code is as follows:


$ Array = [
[1, 2],
[3, 4],
];

Foreach ($ array as list ($ a, $ B )){
Echo "A: $ a; B: $ B \ n ";
}
?>


4. empty () supports user-defined functions.
Parameters in empty () cannot be functions. Now we can.

The code is as follows:


Function foo (){
Return false;
}

If (empty (foo ())){
Echo 11;
} Else {
Echo 12;
}


5 non-variable array and string support subscript acquisition

The code is as follows:



Echo array (1, 2, 3) [0];
Echo [1, 2, 3] [0];

Echo "foobar" [2];

?>


6 class names can be obtained through: class

The code is as follows:


Namespace Name \ Space;
Class ClassName {}

Echo ClassName: class;

Echo "\ n ";
?>


7. added the opcache extension.
Using opcache will improve php performance. you can add static compilation (-- enable-opcache) or dynamic expansion (zend_extension) to this optimization item like other extensions.

Http://www.bkjia.com/PHPjc/327953.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327953.htmlTechArticlePHP5.5 was just released not long ago, what are the new features inside? Official documentation here: http://www.php.net/manual/zh/migration55.new-features.php 1 builder yield keywords in yield...

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.