Home Backend Development PHP Tutorial 解析PHP 5.5 新特性_php技巧

解析PHP 5.5 新特性_php技巧

May 17, 2016 am 08:57 AM
new features

PHP5.5 前不久前刚刚发布,里面的新特性有什么?官方文档在这里:
http://www.php.net/manual/zh/migration55.new-features.php
1 生成器 yield关键字
yield的中文文档在这里:http://php.net/manual/zh/language.generators.overview.php
查看文档,能知道yield的一个功能就是能有效的降低迭代的内存开销。比如官网的这个xrange例子:

复制代码 代码如下:

function xrange($start, $limit, $step = 1) {
    for ($i = $start; $i         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";
?>

这里的xrange是一个迭代,功能和range是一样的,如果使用range函数的话,那么函数内部实现会储存每个迭代的中间过程,即每个中间变量都有个内存空间,那么首先程序使用的内存空间就大了,而且分配内存,回收内存都会导致程序的运行时间加长。但是如果使用上yield实现的xrange函数的话,里面所有的中间变量都只使用一个内存$i,这样节省的时间和空间都会变小。

那么为什么yield会有这样的效果呢?联想到lua中的yield,这里就算是协程的概念了。在lua语言中,当程序运行到yield的时候,使用协程将上下文环境记录住,然后将程序操作权归还到主函数,当主函数调用resume的时候,会重新唤起协程,读取yield记录的上下文。这样形成了程序语言级别的多协程操作。php 5.5这里的yield也是同样的道理,当程序运行到yield的时候,当前程序就唤起协程记录上下文,然后主函数继续操作,只是php中没有使用如resume一样的关键字,而是“在使用的时候唤起”协程。比如上例中的foreach迭代器就能唤起yield。所以上面的这个例子就能理解了。

其实照着引用yield来说,好多内部函数,特别是迭代有关的函数应该都有可能进行优化。或许后续会有yield版本和非yield版本的实现同一功能的函数把。

2 finally关键字
这个和java中的finally一样,经典的try ... catch ... finally 三段式异常处理。

3 foreach 支持list()
对于“数组的数组”进行迭代,之前需要使用两个foreach,现在只需要使用foreach + list了,但是这个数组的数组中的每个数组的个数需要一样。看文档的例子一看就明白了。
复制代码 代码如下:

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

foreach ($array as list($a, $b)) {
    echo "A: $a; B: $b\n";
}
?>

4 empty() 支持自定义函数了
之前empty()中的参数是不能为函数的。现在可以了
复制代码 代码如下:

function foo(){
    return false;
}

if(empty(foo())){
    echo 11;
} else {
    echo 12;
}

5 非变量array和string也能支持下标获取了
复制代码 代码如下:


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

echo "foobar"[2];

?>

6 类名通过::class可以获取
复制代码 代码如下:

namespace Name\Space;
class ClassName {}

echo ClassName::class;

echo "\n";
?>

7 增加了opcache扩展
使用opcache会提高php的性能,你可以和其他扩展一样静态编译(--enable-opcache)或者动态扩展(zend_extension)加入这个优化项。
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1660
14
PHP Tutorial
1260
29
C# Tutorial
1233
24
PHP 8.3 released: new features at a glance PHP 8.3 released: new features at a glance Nov 27, 2023 pm 12:52 PM

PHP8.3 released: Overview of new features As technology continues to develop and needs change, programming languages ​​are constantly updated and improved. As a scripting language widely used in web development, PHP has been constantly improving to provide developers with more powerful and efficient tools. The recently released PHP 8.3 version brings many long-awaited new features and improvements. Let’s take a look at an overview of these new features. Initialization of non-null properties In past versions of PHP, if a class property was not explicitly assigned a value, its value

A guide to learn the new features of PHP8 and gain an in-depth understanding of the latest technology A guide to learn the new features of PHP8 and gain an in-depth understanding of the latest technology Dec 23, 2023 pm 01:16 PM

An in-depth analysis of the new features of PHP8 to help you master the latest technology. As time goes by, the PHP programming language has been constantly evolving and improving. The recently released PHP8 version provides developers with many exciting new features and improvements, bringing more convenience and efficiency to our development work. In this article, we will analyze the new features of PHP8 in depth and provide specific code examples to help you better master these latest technologies. JIT compiler PHP8 introduces JIT (Just-In-Time) compilation

What are the new features of php8 What are the new features of php8 Sep 25, 2023 pm 01:34 PM

New features of php8 include JIT compiler, type deduction, named parameters, union types, properties, error handling improvements, asynchronous programming support, new standard library functions and anonymous class extensions. Detailed introduction: 1. JIT compiler, PHP8 introduces the JIT compiler, which is an important performance improvement. The JIT compiler can compile and optimize some high-frequency execution codes in real time, thereby improving the running speed; 2. Type derivation , PHP8 introduces the type inference function, allowing developers to automatically deduce the type of variables when declaring variables, etc.

Interpretation of new features of Go language: making programming more efficient Interpretation of new features of Go language: making programming more efficient Mar 10, 2024 pm 12:27 PM

[Interpretation of new features of Go language: To make programming more efficient, specific code examples are needed] In recent years, Go language has attracted much attention in the field of software development, and its simple and efficient design concept has attracted more and more developers. As a statically typed programming language, Go language continues to introduce new features to improve development efficiency and simplify the code writing process. This article will provide an in-depth explanation of the latest features of the Go language and discuss how to experience the convenience brought by these new features through specific code examples. Modular development (GoModules) Go language from 1

An overview of the new features of CSS3: How to use CSS3 to achieve transition effects An overview of the new features of CSS3: How to use CSS3 to achieve transition effects Sep 09, 2023 am 11:27 AM

Overview of the new features of CSS3: How to use CSS3 to achieve transition effects CSS3 is the latest version of CSS. Among the many new features, the most interesting and practical one should be the transition effect. Transition effects can make our pages smoother and more beautiful during interaction, giving users a good visual experience. This article will introduce the basic usage of CSS3 transition effects, with corresponding code examples. transition-property attribute: Specify the CSS property transition effect that needs to be transitioned

Overview of the new features of CSS3: How to use CSS3 to achieve horizontally centered layout Overview of the new features of CSS3: How to use CSS3 to achieve horizontally centered layout Sep 09, 2023 pm 04:09 PM

Overview of the new features of CSS3: How to use CSS3 to achieve horizontally centered layout In web design and layout, horizontally centered layout is a common requirement. In the past, we often used complex JavaScript or CSS tricks to achieve this. However, CSS3 introduced some new features that make horizontally centered layouts simpler and more flexible. This article will introduce some new features of CSS3 and provide some code examples to demonstrate how to use CSS3 to achieve horizontally centered layout. 1. Use flexbox to layout fle

New Redis extension introduced in PHP8.1 New Redis extension introduced in PHP8.1 Jul 07, 2023 pm 09:41 PM

The new Redis extension introduced in PHP8.1 With the rapid development of the Internet, a large amount of data needs to be stored and processed. In order to improve the efficiency and performance of data processing, caching has become an indispensable part. In PHP development, Redis, as a high-performance key-value storage system, is widely used in caching and data storage scenarios. In order to further improve the experience of using Redis in PHP, PHP8.1 introduces a new Redis extension. This article will introduce the new functions of this extension and provide

What are the new features of go language? What are the new features of go language? Aug 24, 2023 pm 01:36 PM

The new features of go language are: 1. Go module, used to manage the dependencies of Go language projects; 2. Error handling, adding a new error type error, making error handling more flexible and concise; 3. Context package, used Used to transfer request range values ​​between goroutines; 4. Embedding, that is, one structure can be embedded in another structure; 5. Synchronization package, to better control the synchronization and communication between goroutines; 6. Error value, Better distinguish between different types of errors; 7. Generics allow developers to write more flexibly.

See all articles