私がコンパイルした PHP 7.0 の主な新機能
原文: http://php.net/manual/en/migration70.new-features.php
1.
string、int、float、bool パラメータ宣言をサポートするようになりました。以前は、クラス名、インターフェイス、配列、および Callable
のみがサポートされていました: キャスト モード (デフォルト) と strict です。モード
<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 が必要な場合に 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.96b4fef55684b9312718d5de63fb7121 比較演算子
は、2 つの式の値のサイズを確認します。 = が返されます。 0、< ; Return -1、> Return 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 でサポートされています。
<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 関数 intdiv