我整理的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">'3'</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['user'] and returns 'nobody'</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">'user'</span><span class="ot">]</span> <span class="ot">??</span> <span class="st">'nobody'</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">'user'</span><span class="ot">])</span> <span class="ot">?</span> <span class="kw">$_GET</span><span class="ot">[</span><span class="st">'user'</span><span class="ot">]</span> <span class="ot">:</span> <span class="st">'nobody'</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['user'], $_POST['user'], and</span><span class="co">// 'nobody'.</span><span class="kw">$username</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">'user'</span><span class="ot">]</span> <span class="ot">??</span> <span class="kw">$_POST</span><span class="ot">[</span><span class="st">'user'</span><span class="ot">]</span> <span class="ot">??</span> <span class="st">'nobody'</span><span class="ot">;</span></code>
4. 比较运算符
就是看两个表达式值的大小,三种关系: = 返回0、 返回 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">'ANIMALS'</span><span class="ot">,</span> <span class="ot">[</span> <span class="st">'dog'</span><span class="ot">,</span> <span class="st">'cat'</span><span class="ot">,</span> <span class="st">'bird'</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