LoginSignup
4
3

More than 5 years have passed since last update.

PHP7で「使える!」と思ったこと

Last updated at Posted at 2016-10-28

??演算子 (Null 合体演算子)

値が存在すればその値を、なければ右のオペランドを返す


PHP5
isset($hoge) ? $hoge : '';

PHP7
$hoge ?? '';

スカラー型を宣言できる


function sumOfInts(int ...$ints)
{
    return array_sum($ints);
}

一行目カッコの中のintです

戻り値の型を宣言できる

function arraysSum(array ...$arrays): array
{
    return array_map(function(array $array): int {
        return array_sum($array);
    }, $arrays);
}

一行目の一番右の:arrayってやつです

参考

http://php.net/manual/ja/migration70.new-features.php
http://www.slideshare.net/hnw/phpcon-kansai20150530

その他にも無名クラス宇宙船演算子などありますが、僕があんまし使わなそうだったんで載せません。
どなたか良いユースケースなどありましたら教えてください!

4
3
1

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
4
3