7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP7を触ってみたメモ

Last updated at Posted at 2016-06-03

概要

PHP7を触ってみたメモ。

環境

  • PHP 7.0.7
  • CentOS release 6.7 (Final)

php7のインストール

参考: CentOSにPHP7をインストールする方法

スカラー型宣言

タイプヒンティングにスカラー型が宣言できるようになった。

function sample( int $arg ) {
    return $atg;
}

// OK
sample(1);

// NG
sample('a');

戻り値の型宣言

戻り値の型を宣言することも可能。
ただし、==での判別なので、下記は通ってしまう。

OKパターン
function sample(): string {
    return 12345;
}

sample();

下記のディレクティブを書くことで厳しく判定してくれるようです。

NGパターン
declare(strict_types=1);

function sample(): string {
    return 12345;
}

sample();

致命的なエラーをキャッチできるようになった

Throwable // PHP7~ [interface]
 ├ Exception                  // 通常の例外
 │ └ ErrorException
 └ Error                      // PHP7~
   ├ TypeError              // PHP7~ タイプヒントと異なる型を関数に渡した場合に起こるエラー
   ├ AssertionError         // PHP7~ assert() によるアサーションが失敗した場合に起こるエラー
   ├ ParseError             // PHP7~ 構文解析時(=eval,include時)に起こるエラー
   ├ ArithmeticError        // PHP7~ 数学的な操作によるエラー

引用元: http://yokotakenji.me/log/programming/php/5042/

実験結果
<?php

// Error
try {

    new SampleClass();

} catch( Error $e ) {

    echo sprintf( "[Error] %s\n", $e->getMessage() );

}
// 実行結果: [Error] Class 'SampleClass' not found


// ParseError
try {

    include('parse_error.php');

} catch( ParseError $e ) {

    echo sprintf( "[ParseError] %s\n", $e->getMessage() );

}
// 実行結果: [ParseError] syntax error, unexpected end of file


// TypeError
try {

    function sample( int $num )
    {
        return $num;
    }

    sample('string');

} catch( TypeError $e ) {

    echo sprintf( "[TypeError] %s\n", $e->getMessage() );

}
// 実行結果: [TypeError] Argument 1 passed to sample() must be of the type integer, string given, called in /home/vagrant/script/php7/exception.php on line 36


// AssertionError
// php.iniで、zend.assertionsとassert.exceptionを有効にする必要あり
try {

    // assert_options( ASSERT_EXCEPTION, false );
    assert( false );


} catch( AssertionError $e ) {

    echo sprintf( "[AssertionError] %s\n", $e->getMessage() );

}
// 実行結果: [AssertionError] assert(false)

無名クラス

JavaScriptみたいに即時実行も可能。

echo (new class {

    public function sum( $num1, $num2 )
    {
        return $num1 + $num2;
    }

})->sum(1,2);

// 3

結合比較(スペースシップ)演算子

<=> という比較演算子で、左辺と右辺を比較した結果を -1 0 1のどれかで返す。
rubyやperlでは既に実装されている。
いろいろ試してみた。

echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1


echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1


echo "sample1" <=> "sample1"; // 0
echo "sample1" <=> "sample2"; // -1
echo "sample2" <=> "sample1"; // 1


echo "sample1" <=> "sample1"; // 0
echo "sample1" <=> "asample2"; // 1
echo "asample2" <=> "sample1"; // -1

Null合体演算子

Null合体演算子と呼ばれる??が追加。

$name = $params['name'] ?? 'no name';

下記と等価。

if (isset($params['name'])) {
    $name = $params['name'];
} else {
    $name = 'no name';
}

随時更新するかも。

以上

[参考]
http://qiita.com/rana_kualu/items/68886ba86ee4e75c4122
https://www.infiniteloop.co.jp/blog/2016/04/php7-0-all-rfc/

7
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?