首页 > 后端开发 > php教程 > 小弟我整理的PHP 7.0主要新特性

小弟我整理的PHP 7.0主要新特性

WBOY
发布: 2016-06-13 12:28:19
原创
713 人浏览过

我整理的PHP 7.0主要新特性

原文:http://php.net/manual/en/migration70.new-features.php

1.标量参数类型声明

现在支持字符串(string)、整型(int)、浮点数(float)、及布尔型(bool)参数声明,以前只支持类名、接口、数组及Callable

两种风格:强制转换模式(默认)与严格模式

<code class="sourceCode php"><span class="kw"><?php</span><span class="co">// Coercive mode</span><span class="kw">function</span> sumOfInts<span class="ot">(</span>int <span class="st">...</span><span class="kw">$ints</span><span class="ot">)</span>{    <span class="kw">return</span> <span class="fu">array_sum</span><span class="ot">(</span><span class="kw">$ints</span><span class="ot">);</span>}<span class="fu">var_dump</span><span class="ot">(</span>sumOfInts<span class="ot">(</span><span class="dv">2</span><span class="ot">,</span> <span class="st">&#39;3&#39;</span><span class="ot">,</span> <span class="fl">4.1</span><span class="ot">));</span></code>
登录后复制

2.返回类型声明

<code class="sourceCode php"><span class="kw"><?php</span><span class="kw">function</span> arraysSum<span class="ot">(</span><span class="fu">array</span> <span class="st">...</span><span class="kw">$arrays</span><span class="ot">)</span>: <span class="fu">array</span>{    <span class="kw">return</span> <span class="fu">array_map</span><span class="ot">(</span><span class="kw">function</span><span class="ot">(</span><span class="fu">array</span> <span class="kw">$array</span><span class="ot">)</span>: int {        <span class="kw">return</span> <span class="fu">array_sum</span><span class="ot">(</span><span class="kw">$array</span><span class="ot">);</span>    }<span class="ot">,</span> <span class="kw">$arrays</span><span class="ot">);</span>}<span class="fu">print_r</span><span class="ot">(</span>arraysSum<span class="ot">([</span><span class="dv">1</span><span class="ot">,</span><span class="dv">2</span><span class="ot">,</span><span class="dv">3</span><span class="ot">],</span> <span class="ot">[</span><span class="dv">4</span><span class="ot">,</span><span class="dv">5</span><span class="ot">,</span><span class="dv">6</span><span class="ot">],</span> <span class="ot">[</span><span class="dv">7</span><span class="ot">,</span><span class="dv">8</span><span class="ot">,</span><span class="dv">9</span><span class="ot">]));</span></code>
登录后复制

3.??运算符

?? 用于替代需要isset的场合,这是一个语法糖。

<code class="sourceCode php"><span class="kw"><?php</span><span class="co">// Fetches the value of $_GET[&#39;user&#39;] and returns &#39;nobody&#39;</span><span class="co">// if it does not exist.</span><span class="kw">$username</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;user&#39;</span><span class="ot">]</span> <span class="ot">??</span> <span class="st">&#39;nobody&#39;</span><span class="ot">;</span><span class="co">// This is equivalent to:</span><span class="kw">$username</span> = <span class="fu">isset</span><span class="ot">(</span><span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;user&#39;</span><span class="ot">])</span> <span class="ot">?</span> <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;user&#39;</span><span class="ot">]</span> <span class="ot">:</span> <span class="st">&#39;nobody&#39;</span><span class="ot">;</span><span class="co">// Coalescing can be chained: this will return the first</span><span class="co">// defined value out of $_GET[&#39;user&#39;], $_POST[&#39;user&#39;], and</span><span class="co">// &#39;nobody&#39;.</span><span class="kw">$username</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;user&#39;</span><span class="ot">]</span> <span class="ot">??</span> <span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;user&#39;</span><span class="ot">]</span> <span class="ot">??</span> <span class="st">&#39;nobody&#39;</span><span class="ot">;</span></code>
登录后复制

4.<=> 比较运算符

就是看两个表达式值的大小,三种关系: = 返回0、< 返回-1、 > 返回 1

<code class="sourceCode php"><span class="kw"><?php</span><span class="co">// Integers</span><span class="fu">echo</span> <span class="dv">1</span> <=> <span class="dv">1</span><span class="ot">;</span> <span class="co">// 0</span><span class="fu">echo</span> <span class="dv">1</span> <=> <span class="dv">2</span><span class="ot">;</span> <span class="co">// -1</span><span class="fu">echo</span> <span class="dv">2</span> <=> <span class="dv">1</span><span class="ot">;</span> <span class="co">// 1</span><span class="co">// Floats</span><span class="fu">echo</span> <span class="fl">1.5</span> <=> <span class="fl">1.5</span><span class="ot">;</span> <span class="co">// 0</span><span class="fu">echo</span> <span class="fl">1.5</span> <=> <span class="fl">2.5</span><span class="ot">;</span> <span class="co">// -1</span><span class="fu">echo</span> <span class="fl">2.5</span> <=> <span class="fl">1.5</span><span class="ot">;</span> <span class="co">// 1</span> <span class="co">// Strings</span><span class="fu">echo</span> <span class="st">"a"</span> <=> <span class="st">"a"</span><span class="ot">;</span> <span class="co">// 0</span><span class="fu">echo</span> <span class="st">"a"</span> <=> <span class="st">"b"</span><span class="ot">;</span> <span class="co">// -1</span><span class="fu">echo</span> <span class="st">"b"</span> <=> <span class="st">"a"</span><span class="ot">;</span> <span class="co">// 1</span></code>
登录后复制

5.define支持定义数组类型的值

php 5.6已经支持CONST 语法定义数组类的常量,PHP7中支持define语法。

<code class="sourceCode php"><span class="kw"><?php</span><span class="fu">define</span><span class="ot">(</span><span class="st">&#39;ANIMALS&#39;</span><span class="ot">,</span> <span class="ot">[</span>    <span class="st">&#39;dog&#39;</span><span class="ot">,</span>    <span class="st">&#39;cat&#39;</span><span class="ot">,</span>    <span class="st">&#39;bird&#39;</span><span class="ot">]);</span><span class="fu">echo</span> <span class="kw">ANIMALS</span><span class="ot">[</span><span class="dv">1</span><span class="ot">];</span> <span class="co">// outputs "cat"</span></code>
登录后复制

6.匿名类

<code class="sourceCode php"><span class="kw"><?php</span><span class="kw">interface</span> Logger {    <span class="kw">public</span> <span class="kw">function</span> <span class="fu">log</span><span class="ot">(</span>string <span class="kw">$msg</span><span class="ot">);</span>}<span class="kw">class</span> Application {    <span class="kw">private</span> <span class="kw">$logger</span><span class="ot">;</span>    <span class="kw">public</span> <span class="kw">function</span> getLogger<span class="ot">()</span>: Logger {         <span class="kw">return</span> <span class="kw">$this</span>->logger<span class="ot">;</span>    }    <span class="kw">public</span> <span class="kw">function</span> setLogger<span class="ot">(</span>Logger <span class="kw">$logger</span><span class="ot">)</span> {         <span class="kw">$this</span>->logger = <span class="kw">$logger</span><span class="ot">;</span>    }}<span class="kw">$app</span> = <span class="kw">new</span> Application<span class="ot">;</span><span class="kw">$app</span>->setLogger<span class="ot">(</span><span class="kw">new</span> <span class="kw">class</span> <span class="kw">implements</span> Logger {    <span class="kw">public</span> <span class="kw">function</span> <span class="fu">log</span><span class="ot">(</span>string <span class="kw">$msg</span><span class="ot">)</span> {        <span class="fu">echo</span> <span class="kw">$msg</span><span class="ot">;</span>    }}<span class="ot">);</span><span class="fu">var_dump</span><span class="ot">(</span><span class="kw">$app</span>->getLogger<span class="ot">());</span></code>
登录后复制

7.增加了整除函数 intdiv

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板