Home > php教程 > php手册 > body text

PHP5.5的新特性

WBOY
Release: 2016-06-13 11:29:23
Original
1065 people have browsed it

看了@轩脉刃 今天出炉的PHP 5.5 新特性。不过没有翻译全,我这里稍微补充下,整理成完整的一篇:)

原文:http://www.php.net/manual/zh/migration55.new-features.php

1,生成器

php5.5通过引入yield关键字来支持generators。Generators提供了一种更加简洁的生成迭代器的方式,无需通过笨重复杂的Iterator接口。

一个简单的示例:利用generators实现和range函数一样的迭代功能

<span function</span> xrange(<span $start</span>, <span $limit</span>, <span $step</span> = 1<span ) {
    </span><span for</span> (<span $i</span> = <span $start</span>; <span $i</span> <= <span $limit</span>; <span $i</span> += <span $step</span><span ) {
        yield </span><span $i</span><span ;
    }
}

</span><span echo</span> 'Single digit odd numbers: '<span ;

</span><span /*</span><span 
 * Note that an array is never created or returned,
 * which saves memory.
 </span><span */</span>
<span foreach</span> (xrange(1, 9, 2) <span as</span> <span $number</span><span ) {
    </span><span echo</span> "<span $number</span> "<span ;
}

</span><span echo</span> "\n";
Copy after login

以上例程会输出:

Single digit odd numbers: 1 3 5 7 9

2,添加finally关键字

try-catch语句现在支持后边添加finally。无论是否会抛出异常,finally中的代码总是会被执行。

3,新的哈希API

添加了一个新的hash函数API,它可以更简单的生成安全的散列值,并且能够像crypt()一样用于密码验证管理。详细请参阅:password_hash()

4,foreach支持list()

现在,foreach能够利用list()结构分解被嵌套的数组,并且提取其中的值。举例来说:

<span $array</span> =<span  [
    [</span>1, 2],<span 
    [</span>3, 4],<span 
];

</span><span foreach</span> (<span $array</span> <span as</span> <span list</span>(<span $a</span>, <span $b</span><span )) {
    </span><span echo</span> "A: <span $a</span>; B: <span $b</span>\n"<span ;
}</span>
Copy after login

以上例程会输出:

A: 1; B: 2
A: 3; B: 4

更多文档可以参见foreach手册。

5,empty()支持表达式

现在可以将任意的表达式传入empty()中。例如:

<span function</span><span  always_false() {
    </span><span return</span> <span false</span><span ;
}

</span><span if</span> (<span empty</span><span (always_false())) {
    </span><span echo</span> "This will be printed.\n"<span ;
}

</span><span if</span> (<span empty</span>(<span true</span><span )) {
    </span><span echo</span> "This will not be printed.\n"<span ;
}</span>
Copy after login

以上例程会输出:

This will be printed.

6,array和string字面的dereferencing

可以利用下标访问array、string字面中的某个元素或者char。(ps:dereferencing不知道怎么翻译才准确...看了轩脉刃的表述:支持用下标获取,呵呵,其实就是这个意思。)

<span echo</span> 'Array dereferencing: '<span ;
</span><span echo</span> [1, 2, 3][0<span ];
</span><span echo</span> "\n"<span ;

</span><span echo</span> 'String dereferencing: '<span ;
</span><span echo</span> 'PHP'[0<span ];
</span><span echo</span> "\n";
Copy after login

以上例程会输出:

Array dereferencing: 1
String dereferencing: P

7,通过::class获取类名

可以利用ClassName::class 获取class完整的类名。例如:

<span namespace Name\Space;
</span><span class</span><span  ClassName {}

</span><span echo</span> ClassName::<span class</span><span ;

</span><span echo</span> "\n";
Copy after login

以上例程会输出:

Name\Space\ClassName

8,新增扩展OPcache

Zend Optimiser+的opcode cache被加入了php扩展,成为OPcache extension。OPcache可以提高php的执行性能,它能够在共享内存中存储编译好的opcode,以此节约原本为每个请求加载\解析php脚本的时间。具体安装和使用详情请参考the installation instructions。

9,foreach支持非标量key

foreach现在支持任何类型的key。原生的php数组仅能够用标量作为key,而现在可以通过实现Iterator::key()来返回任意类型的数据,并且foreach均能接收。

10,在win平台上支持Apache 2.4

windows上用于apache2.4的SAPI已被实现支持。

11,改进GD

GD扩展做出了很多改进,包括:

imageflip()函数

imagecrop()imagecropauto() 函数

imagecreatefromwebp()imagewebp() 函数

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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template