> php教程 > php手册 > 본문

PHP 5.3 5.4 5.5新特性(三)

WBOY
풀어 주다: 2016-06-06 19:58:32
원래의
1036명이 탐색했습니다.

3.PHP 5.5中的新特性 3.1.PHP 5.5中的新特性 新特性及提案列表 都相当大,而且不是按重要性排序。所以,如果你不想通读一遍的话,这里有四个特点我个人是最兴奋的: :一个简单的密码散列API :标量类型提示 :Getter和Setter :生成器 现在,让我们来看看PHP5.5

3.PHP 5.5中的新特性

3.1.PHP 5.5中的新特性

新特性及提案列表 都相当大,而且不是按重要性排序。所以,如果你不想通读一遍的话,这里有四个特点我个人是最兴奋的:
:一个简单的密码散列API
:标量类型提示
:Getter和Setter
:生成器
现在,让我们来看看PHP5.5 可能会新增的功能:
3.1.1、放弃对Windows XP和2003 的支持
3.1.2.弃用e修饰符

e修饰符是指示preg_replace函数用来评估替换字符串作为PHP代码,而不只是仅仅做一个简单的字符串替换。不出所料,这种行为会源源不断的出现安全问题。这就是为什么在PHP5.5 中使用这个修饰符将抛出一个弃用警告。作为替代,你应该使用preg_replace_callback函数。你可以从RFC找到更多关于这个变化相应的信息。3.1.3.新增函数和类boolval()

PHP已经实现了strval、intval和floatval的函数。为了达到一致性将添加boolval函数。它完全可以作为一个布尔值计算,也可以作为一个回调函数。
hash_pbkdf2()

PBKDF2全称“Password-Based Key Derivation Function 2”,正如它的名字一样,是一种从密码派生出加密密钥的算法。这就需要加密算法,也可以用于对密码哈希。更广泛的说明和用法示例
array_column()

<ol>
<li><span>//从数据库获取一列,但返回是数组。  </span></li>
<li>
<span>        $userNames </span><span>=</span><span>[];</span>
</li>
<li>
<span>foreach</span><span>(</span><span>$users </span><span>as</span><span> $user</span><span>)</span><span>{</span>
</li>
<li>
<span>            $userNames</span><span>[]</span><span>=</span><span> $user</span><span>[</span><span>'name'</span><span>];</span>
</li>
<li><span>}</span></li>
<li><span>//以前获取数组某列值,现在如下  </span></li>
<li>
<span>        $userNames </span><span>=</span><span> array_column</span><span>(</span><span>$users</span><span>,</span><span>‘</span><span>name</span><span>’);</span>
</li>
</ol>
로그인 후 복사

intl 扩展

将有许多改进 intl的扩展。例如,将会有新的IntlCalendar,IntlGregorianCalendar,IntlTimeZone,IntlBreakIterator,IntlRuleBasedBreakIterator,IntlCodePointBreakIterator类。之前,我竟然不知道有这么多关于intl扩展,如果你想知道更多,我建议你去最新公告里找 Calendar和 BreakIterator。

3.1.4.一个简单的密码散列API

<ol>
<li>
<span>        $password </span><span>=</span><span>“</span><span>foo</span><span>”;</span>
</li>
<li><span>// creating the hash    </span></li>
<li>
<span>        $hash </span><span>=</span><span> password_hash</span><span>(</span><span>$password</span><span>,</span><span> PASSWORD_BCRYPT</span><span>);</span>
</li>
<li><span>// verifying a password    </span></li>
<li>
<span>if</span><span>(</span><span>password_verify</span><span>(</span><span>$password</span><span>,</span><span> $hash</span><span>))</span><span>{</span>
</li>
<li><span>// password correct!    </span></li>
<li>
<span>}</span><span>else</span><span>{</span>
</li>
<li><span>// password wrong!    </span></li>
<li><span>}</span></li>
</ol>
로그인 후 복사

3.1.5.新的语言特性和增强功能。
常量引用

“常量引用”意味着数组可以直接操作字符串和数组字面值。举两个例子:

<ol>
<li>
<span>function</span><span> randomHexString</span><span>(</span><span>$length</span><span>)</span><span>{</span>
</li>
<li>
<span>            $str </span><span>=</span><span>”;</span>
</li>
<li>
<span>for</span><span>(</span><span>$i </span><span>=</span><span>0</span><span>;</span><span> $i </span><span><span> $length</span><span>;</span><span>++</span><span>$i</span><span>)</span><span>{</span><span> $str </span><span>.=</span><span>“</span><span>0123456789abcdef</span><span>”[</span><span>mt_rand</span><span>(</span><span>0</span><span>,</span><span>15</span><span>)];</span><span>// direct dereference of string } } function randomBool() { return [false, true][mt_rand(0, 1)]; // direct dereference of array } </span></span>
</li>
</ol>
로그인 후 복사

我不认为在实践中会使用此功能,但它使语言更加一致。请参阅 RFC。

3.1.6.调用empty()函数(和其他表达式)一起工作

目前,empty()语言构造只能用在变量,而不能在其他表达式。
在特定的代码像empty($this->getFriends())将会抛出一个错误。作为PHP5.5 这将成为有效的代码
3.1.7.获取完整类别名称

PHP5.3 中引入命名空间的别名类和命名空间短版本的功能。虽然这并不适用于字符串类名称

<ol>
<li>
<span>use</span><span>Some</span><span>\Deeply\Nested\Namespace\FooBar</span><span>;</span>
</li>
<li><span>// does not work, because this will try to use the global `FooBar` class    </span></li>
<li>
<span>        $reflection </span><span>=</span><span>new</span><span>ReflectionClass</span><span>(‘</span><span>FooBar</span><span>’);</span>
</li>
<li>
<span>        echo </span><span>FooBar</span><span>::</span><span>class</span><span>;</span>
</li>
</ol>
로그인 후 복사

为了解决这个问题采用新的FooBar::class语法,它返回类的完整类别名称
3.1.8.参数跳跃

如果你有一个函数接受多个可选的参数,目前没有办法只改变最后一个参数,而让其他所有参数为默认值。
RFC上的例子,如果你有一个函数如下:

<ol><li>
<span>function</span><span> create_query</span><span>(</span><span>$where</span><span>,</span><span> $order_by</span><span>,</span><span> $join_type</span><span>=”,</span><span> $execute </span><span>=</span><span>false</span><span>,</span><span> $report_errors </span><span>=</span><span>true</span><span>)</span><span>{</span><span>…</span><span>}</span>
</li></ol>
로그인 후 복사

那么有没有办法设置$report_errors=false,而其他两个为默认值。为了解决这个跳跃参数的问题而提出:

<ol><li>
<span>        create_query</span><span>(“</span><span>deleted</span><span>=</span><span>0</span><span>″,</span><span>“</span><span>name</span><span>”,</span><span>default</span><span>,</span><span>default</span><span>,</span><span>false</span><span>);</span>
</li></ol>
로그인 후 복사

我个人不是特别喜欢这个提议。在我的眼睛里,代码需要这个功能,只是设计不当。函数不应该有12个可选参数。
3.1.9.标量类型提示

标量类型提示原本计划进入5.4,但由于缺乏共识而没有做。获取更多关于为什么标量类型提示没有做进PHP的信息,请参阅: 标量类型提示比你认为的更难。
对于PHP5.5 而言,针对标量类型提示讨论又一次出现,我认为这是一个相当不错的 提议。
它需要通过输入值来指定类型。例如:123,123.0,“123”都是一个有效的int参数输入,但“hello world”就不是。这与内部函数的行为一致。

<ol>
<li>
<span>function</span><span> foo</span><span>(</span><span>int</span><span> $i</span><span>)</span><span>{</span><span>…</span><span>}</span>
</li>
<li>
<span>        foo</span><span>(</span><span>1</span><span>);</span><span>// $i = 1  </span>
</li>
<li>
<span>        foo</span><span>(</span><span>1.0</span><span>);</span><span>// $i = 1  </span>
</li>
<li>
<span>        foo</span><span>(“</span><span>1</span><span>″);</span><span>// $i = 1  </span>
</li>
<li>
<span>        foo</span><span>(“</span><span>1abc</span><span>”);</span><span>// not yet clear, maybe $i = 1 with notice  </span>
</li>
<li>
<span>        foo</span><span>(</span><span>1.5</span><span>);</span><span>// not yet clear, maybe $i = 1 with notice  </span>
</li>
<li>
<span>        foo</span><span>([]);</span><span>// error  </span>
</li>
<li>
<span>        foo</span><span>(“</span><span>abc</span><span>”);</span><span>// error  </span>
</li>
</ol>
로그인 후 복사

3.1.10.Getter 和 Setter

如果你从不喜欢写这些getXYZ()和setXYZ($value)方法,那么这应该是你最受欢迎的改变。提议添加一个新的语法来定义一个属性的设置/读取:

<ol>
<li>
<span></span><span>php  </span>
</li>
<li>
<span>class</span><span>TimePeriod</span><span>{</span>
</li>
<li>
<span>public</span><span> $seconds</span><span>;</span>
</li>
<li>
<span>public</span><span> $hours </span><span>{</span>
</li>
<li>
<span>get</span><span>{</span><span>return</span><span> $this</span><span>-></span><span>seconds </span><span>/</span><span>3600</span><span>;</span><span>}</span>
</li>
<li>
<span>set</span><span>{</span><span> $this</span><span>-></span><span>seconds </span><span>=</span><span> $value </span><span>*</span><span>3600</span><span>;</span><span>}</span>
</li>
<li><span>}</span></li>
<li><span>}</span></li>
<li>
<span>        $timePeriod </span><span>=</span><span>new</span><span>TimePeriod</span><span>;</span>
</li>
<li>
<span>        $timePeriod</span><span>-></span><span>hours </span><span>=</span><span>10</span><span>;</span>
</li>
<li>
<span>        var_dump</span><span>(</span><span>$timePeriod</span><span>-></span><span>seconds</span><span>);</span><span>// int(36000)  </span>
</li>
<li>
<span>        var_dump</span><span>(</span><span>$timePeriod</span><span>-></span><span>hours</span><span>);</span><span>// int(10)</span>
</li>
</ol>
로그인 후 복사

当然还有更多的功能,比如只读属性。如果你想要知道更多,请参阅 RFC。
3.1.11.生成器 yield

目前,自定义迭代器很少使用,因为它们的实现,需要大量的样板代码。生成器解决这个问题,并提供了一种简单的样板代码来创建迭代器。 例如,你可以定义一个范围函数作为迭代器:

<ol>
<li>
<span></span><span>php  </span>
</li>
<li>
<span>function</span><span>*</span><span>xrange</span><span>(</span><span>$start</span><span>,</span><span> $end</span><span>,</span><span> $step </span><span>=</span><span>1</span><span>)</span><span>{</span>
</li>
<li>
<span>for</span><span>(</span><span>$i </span><span>=</span><span> $start</span><span>;</span><span> $i </span><span><span> $end</span><span>;</span><span> $i </span><span>+=</span><span> $step</span><span>)</span><span>{</span><span>yield</span><span> $i</span><span>;</span><span>}</span><span>}</span><span>foreach</span><span>(</span><span>xrange</span><span>(</span><span>10</span><span>,</span><span>20</span><span>)</span><span>as</span><span> $i</span><span>)</span><span>{</span><span>// … } </span></span>
</li>
</ol>
로그인 후 복사

上述xrange函数具有与内建函数相同的行为,但有一点区别:不是返回一个数组的所有值,而是返回一个迭代器动态生成的值。

3.1.12.列表解析和生成器表达式

列表解析提供一个简单的方法对数组进行小规模操作:

<ol><li>
<span>    $firstNames </span><span>=</span><span>[</span><span>foreach</span><span>(</span><span>$users </span><span>as</span><span> $user</span><span>)</span><span>yield</span><span> $user</span><span>-></span><span>firstName</span><span>];</span>
</li></ol>
로그인 후 복사

上述列表解析相等于下面的代码:

<ol>
<li>
<span>    $firstNames </span><span>=</span><span>[];</span>
</li>
<li>
<span>foreach</span><span>(</span><span>$users </span><span>as</span><span> $user</span><span>)</span><span>{</span>
</li>
<li>
<span>        $firstNames</span><span>[]</span><span>=</span><span> $user</span><span>-></span><span>firstName</span><span>;</span>
</li>
<li><span>}</span></li>
</ol>
로그인 후 복사

也可以这样过滤数组:

<ol><li>
<span>    $underageUsers </span><span>=</span><span>[</span><span>foreach</span><span>(</span><span>$users </span><span>as</span><span> $user</span><span>)</span><span>if</span><span>(</span><span>$user</span><span>-></span><span>age </span><span><span>18</span><span>)</span><span>yield</span><span> $user</span><span>];</span></span>
</li></ol>
로그인 후 복사

生成器表达式也很类似,但是返回一个迭代器(用于动态生成值)而不是一个数组。

以上来自:http://www.oschina.net/question/157182_61259
3.1.13.finally关键字

这个和java中的finally一样,经典的try … catch … finally 三段式异常处理。
3.1.14.foreach 支持list()

对于“数组的数组”进行迭代,之前需要使用两个foreach,现在只需要使用foreach + list了,但是这个数组的数组中的每个数组的个数需要一样。看文档的例子一看就明白了。

<ol>
<li>
<span>    $array </span><span>=</span><span>[</span>
</li>
<li>
<span>[</span><span>1</span><span>,</span><span>2</span><span>],</span>
</li>
<li>
<span>[</span><span>3</span><span>,</span><span>4</span><span>],</span>
</li>
<li><span>];</span></li>
<li>
<span>foreach</span><span>(</span><span>$array </span><span>as</span><span> list</span><span>(</span><span>$a</span><span>,</span><span> $b</span><span>))</span><span>{</span>
</li>
<li>
<span>        echo </span><span>“</span><span>A</span><span>:</span><span> $a</span><span>;</span><span> B</span><span>:</span><span> $b\n</span><span>”;</span>
</li>
<li><span>}</span></li>
</ol>
로그인 후 복사

3.1.15.增加了opcache扩展

使用opcache会提高php的性能,你可以和其他扩展一样静态编译(–enable-opcache)或者动态扩展(zend_extension)加入这个优化项。
3.1.16.非变量array和string也能支持下标获取了

<ol>
<li>
<span>    echo array</span><span>(</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3</span><span>)[</span><span>0</span><span>];</span>
</li>
<li>
<span>    echo </span><span>[</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3</span><span>][</span><span>0</span><span>];</span>
</li>
<li>
<span>    echo </span><span>“</span><span>foobar</span><span>”[</span><span>2</span><span>];</span>
</li>
</ol>
로그인 후 복사

更多:http://www.webyang.net/Html/web/article_122.html

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿