Изменения и новшества PHP 5.6.0 RC1

php-logo[1]Вышел первый кандидат в релизы PHP 5.6.0. Можно помочь разработчикам, проверив свои проекты на этой версии (не на боевых серверах, конечно) и сообщив о проблемах.

Изменения радуют. Рассмотрим самое интересное.

Можно использовать простые выражения в константах и значениях по умолчанию:

<span ="php-hl-reserved" style="font-weight: bold; color: #000080;"></span> <span ="php-hl-identifier">PostCache</span>
<span ="php-hl-brackets">{</span>
    <span ="php-hl-reserved" style="font-weight: bold; color: #000080;">const</span> <span ="php-hl-identifier">EXPIRE_HOUR</span><span ="php-hl-"> = </span><span class="php-hl-number" style="color: #0000ff;">60</span><span class="php-hl-"> * </span><span class="php-hl-number" style="color: #0000ff;">60</span><span class="php-hl-">;
    </span><span class="php-hl-reserved" style="font-weight: bold; color: #000080;">const</span> <span class="php-hl-identifier">EXPIRE_DAY</span><span class="php-hl-"> = </span><span class="php-hl-identifier">self</span><span class="php-hl-">::</span><span class="php-hl-identifier">EXPIRE_HOUR</span><span class="php-hl-"> * </span><span class="php-hl-number" style="color: #0000ff;">24</span><span class="php-hl-">;
    </span><span class="php-hl-reserved" style="font-weight: bold; color: #000080;">public</span> <span class="php-hl-reserved" style="font-weight: bold; color: #000080;"></span> <span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var" style="color: #6d3206;">$data</span><span class="php-hl-">, </span><span class="php-hl-var" style="color: #6d3206;">$expire</span><span class="php-hl-"> = </span><span class="php-hl-number" style="color: #0000ff;">3</span><span class="php-hl-"> * </span><span class="php-hl-identifier">EXPIRE_HOUR</span><span class="php-hl-brackets">)</span> <span class="php-hl-brackets">{</span>
        <span class="php-hl-comment" style="color: #777777;">//</span><span class="php-hl-comment" style="color: #777777;"> ...</span>
    <span class="php-hl-brackets">}</span>
<span class="php-hl-brackets">}</span>

Аргументы переменной длины:

<span class="php-hl-reserved" style="font-weight: bold; color: #000080;">function</span> <span class="php-hl-identifier">queue</span><span class="php-hl-brackets">(</span><span class="php-hl-var" style="color: #6d3206;">$name</span><span class="php-hl-code">, </span><span class="php-hl-var" style="color: #6d3206;">$delay</span><span class="php-hl-code"> = </span><span class="php-hl-reserved" style="font-weight: bold; color: #000080;">null</span><span class="php-hl-code">, ...</span><span class="php-hl-var" style="color: #6d3206;">$data</span><span class="php-hl-brackets">)</span>
<span class="php-hl-brackets">{</span>
    <span class="php-hl-reserved" style="font-weight: bold; color: #000080;">foreach</span> <span class="php-hl-brackets">(</span><span class="php-hl-var" style="color: #6d3206;">$data</span> <span class="php-hl-reserved" style="font-weight: bold; color: #000080;">as</span> <span class="php-hl-var" style="color: #6d3206;">$item</span><span class="php-hl-brackets">)</span> <span class="php-hl-brackets">{</span>
        <span class="php-hl-identifier">Queue</span><span class="php-hl-code">::</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var" style="color: #6d3206;">$name</span><span class="php-hl-code">, </span><span class="php-hl-var" style="color: #6d3206;">$item</span><span class="php-hl-code">, </span><span class="php-hl-var" style="color: #6d3206;">$delay</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
    </span><span class="php-hl-brackets">}</span>
<span class="php-hl-brackets">}</span>

Можно на лету распаковать массив в аргументы функции:

<span class="php-hl-reserved" style="font-weight: bold; color: #000080;">function</span> <span class="php-hl-identifier">updatePerson</span><span class="php-hl-brackets">(</span><span class="php-hl-var" style="color: #6d3206;">$id</span><span class="php-hl-code">, </span><span class="php-hl-var" style="color: #6d3206;">$firstName</span><span class="php-hl-code">, </span><span class="php-hl-var" style="color: #6d3206;">$lastName</span><span class="php-hl-brackets">)</span>
<span class="php-hl-brackets">{</span>
    <span class="php-hl-comment" style="color: #777777;">//</span><span class="php-hl-comment" style="color: #777777;"> ...</span>
<span class="php-hl-brackets">}</span>
<span class="php-hl-var" style="color: #6d3206;">$person</span><span class="php-hl-code"> = </span><span class="php-hl-brackets">[</span><span class="php-hl-quotes" style="color: #009933;">'</span><span class="php-hl-string" style="color: #009933;">Alexander</span><span class="php-hl-quotes" style="color: #009933;">'</span><span class="php-hl-code">, </span><span class="php-hl-quotes" style="color: #009933;">'</span><span class="php-hl-string" style="color: #009933;">Makarov</span><span class="php-hl-quotes" style="color: #009933;">'</span><span class="php-hl-brackets">]</span><span class="php-hl-code">;
</span><span class="php-hl-identifier">updatePerson</span><span class="php-hl-brackets">(</span><span class="php-hl-number" style="color: #0000ff;">42</span><span class="php-hl-code">, ...</span><span class="php-hl-var" style="color: #6d3206;">$person</span><span class="php-hl-brackets">)</span>

Ввели оператор для возведения в степень:

<span class="php-hl-reserved" style="font-weight: bold; color: #000080;">echo</span> <span class="php-hl-number" style="color: #0000ff;">2</span><span class="php-hl-code"> ** </span><span class="php-hl-number" style="color: #0000ff;">8</span><span class="php-hl-code">; </span><span class="php-hl-comment" style="color: #777777;">//</span><span class="php-hl-comment" style="color: #777777;"> 256</span>
<span class="php-hl-var" style="color: #6d3206;">$n</span><span class="php-hl-code"> = </span><span class="php-hl-number" style="color: #0000ff;">2</span><span class="php-hl-code">;
</span><span class="php-hl-var" style="color: #6d3206;">$n</span><span class="php-hl-code"> **= </span><span class="php-hl-number" style="color: #0000ff;">8</span><span class="php-hl-code">;
</span><span class="php-hl-reserved" style="font-weight: bold; color: #000080;">echo</span> <span class="php-hl-var" style="color: #6d3206;">$n</span><span class="php-hl-code">; </span><span class="php-hl-comment" style="color: #777777;">//</span><span class="php-hl-comment" style="color: #777777;"> 256</span>

При помощи use теперь можно импортировать функции и константы:

<span class="php-hl-identifier">namespace</span> <span class="php-hl-identifier">My</span><span class="php-hl-code">\</span><span class="php-hl-identifier">Space</span> <span class="php-hl-brackets">{</span>
    <span class="php-hl-reserved" style="font-weight: bold; color: #000080;">const</span> <span class="php-hl-identifier">FOO</span><span class="php-hl-code"> = </span><span class="php-hl-number" style="color: #0000ff;">42</span><span class="php-hl-code">;
    </span><span class="php-hl-reserved" style="font-weight: bold; color: #000080;">function</span> <span class="php-hl-identifier">f</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span> <span class="php-hl-brackets">{</span> <span class="php-hl-reserved" style="font-weight: bold; color: #000080;">echo</span> <span class="php-hl-reserved" style="font-weight: bold; color: #000080;">__FUNCTION__</span><span class="php-hl-code">.</span><span class="php-hl-quotes" style="color: #009933;">"</span><span class="php-hl-special" style="font-weight: bold; color: #000080;">\n</span><span class="php-hl-quotes" style="color: #009933;">"</span><span class="php-hl-code">; </span><span class="php-hl-brackets">}</span>
<span class="php-hl-brackets">}</span>

<span class="php-hl-comment" style="color: #777777;">//</span><span class="php-hl-comment" style="color: #777777;"> global namespace</span>
<span class="php-hl-identifier">namespace</span> <span class="php-hl-brackets">{</span>
    <span class="php-hl-identifier">use</span> <span class="php-hl-reserved" style="font-weight: bold; color: #000080;">const</span> <span class="php-hl-identifier">My</span><span class="php-hl-code">\</span><span class="php-hl-identifier">Space</span><span class="php-hl-code">\</span><span class="php-hl-identifier">FOO</span><span class="php-hl-code">;
    </span><span class="php-hl-identifier">use</span> <span class="php-hl-reserved" style="font-weight: bold; color: #000080;">function</span> <span class="php-hl-identifier">My</span><span class="php-hl-code">\</span><span class="php-hl-identifier">Space</span><span class="php-hl-code">\</span><span class="php-hl-identifier">f</span><span class="php-hl-code">;
    </span><span class="php-hl-reserved" style="font-weight: bold; color: #000080;">echo</span> <span class="php-hl-identifier">FOO</span><span class="php-hl-code">.</span><span class="php-hl-quotes" style="color: #009933;">"</span><span class="php-hl-special" style="font-weight: bold; color: #000080;">\n</span><span class="php-hl-quotes" style="color: #009933;">"</span><span class="php-hl-code">;
    </span><span class="php-hl-identifier">f</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-brackets">}</span>

Об остальных изменениях можно почитать на соответствующей странице документации, которая ещё будет дописываться.

Оригинал: http://rmcreative.ru/blog/post/php-5.6.0rc1


Запись навигация

Top