Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PHP8.xの新機能を使ってより良いコードを書こう!
Search
Shunsuke Santo
February 15, 2023
Programming
0
360
PHP8.xの新機能を使ってより良いコードを書こう!
Shunsuke Santo
February 15, 2023
Tweet
Share
Other Decks in Programming
See All in Programming
15年目のiOSアプリを1から作り直す技術
teakun
1
610
encoding/json/v2のUnmarshalはこう変わった:内部実装で見る設計改善
kurakura0916
0
360
「やめとこ」がなくなった — 1月にZennを始めて22本書いた AI共創開発のリアル
atani14
0
360
コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions
handlename
0
170
AIとペアプロして処理時間を97%削減した話 #pyconshizu
kashewnuts
1
210
go directiveを最新にしすぎないで欲しい話──あるいは、Go 1.26からgo mod initで作られるgo directiveの値が変わる話 / Go 1.26 リリースパーティ
arthur1
2
510
ふつうのRubyist、ちいさなデバイス、大きな一年 / Ordinary Rubyists, Tiny Devices, Big Year
chobishiba
1
410
AI時代のソフトウェア開発でも「人が仕様を書く」から始めよう-医療IT現場での実践とこれから
koukimiura
0
140
RubyとGoでゼロから作る証券システム: 高信頼性が求められるシステムのコードの外側にある設計と運用のリアル
free_world21
0
240
nilとは何か 〜interfaceの構造とnil!=nilから理解する〜
kuro_kurorrr
3
1.8k
Docコメントで始める簡単ガードレール
keisukeikeda
1
100
maplibre-gl-layers - 地図に移動体たくさん表示したい
kekyo
PRO
0
220
Featured
See All Featured
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
140
Ruling the World: When Life Gets Gamed
codingconduct
0
160
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
120
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
140
Between Models and Reality
mayunak
2
230
Docker and Python
trallard
47
3.8k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
140
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
280
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
260
The Curse of the Amulet
leimatthew05
1
9.7k
Chasing Engaging Ingredients in Design
codingconduct
0
130
Transcript
PHP8.xの新機能を使って より良いコードを書こう! 山藤駿亮 Qiita: @SanQ Twitter: @SAshunchan
PHPは7→8へ このLTではPHP8.0からの新機能を紹介
今回紹介する新機能 • コンストラクタ • 名前付き引数 • Match式
コンストラクタ(8.0~) <機能> • プロパティのアクセス権を引数内で 設定できる • プロパティへの代入をコンストラクタ の引数内で行える → コンストラクタにかける記述量が減少
例:string(10) "hello! PHP" を出力 PHP 7.4
例:string(10) "hello! PHP" を出力 PHP 8.0
名前付き引数 (8.0~) <機能> • メソッドの引数に名前を付けられる • メソッドの引数の順序を守らなくても良い • デフォルトの値がある引数は実行時に 値を入れる必要がない → 使用される引数の内容がより分かりや
すい
例:全ての引数を合計した結果を出力 PHP 7.4 PHP 8.0
名前付き引数の注意点 名前付き引数を使用した場合、その後の 引数も名前付き引数を使用しなくてはな らない 上の結果 Error: Cannot use positional argument
after named argument
Match式 (8.0~) <機能> • 条件分岐を文ではなく式として処理する • 条件部分に条件外の値が来た場合エ ラーを吐く • 値の比較は厳密な比較 → 少ない記述かつ厳密な比較で条件分
岐を実装可能になった
例:$numberの値によって$resultの値を変更 PHP 7.4 PHP 8.0
Match式の注意点 • switchは緩やかな比較である一方で Match式は厳密な比較。※ • match式内部で式の実行はできない → switchからmatch式に書き換える際は 注意が必要 ※引数にtrueを入れることでmatch式でも緩やかな比較が可能
最後に… • より柔軟な型の定義やコードの短縮が 可能に! • パフォーマンスが向上! 一部の仕様が変更しているためバージョ ンアップの際は公式のドキュメントを参照 することが重要
参考資料 PHP Supported Versions (https://www.php.net/supported-versions.php) PHP8 released(https://www.php.net/releases/8.0/ja.php) PHP match() (https://www.php.net/manual/ja/control-
structures.match.php) Qiita @rana_kualu 【PHP8.0】PHP8.0の新機能 (https://qiita.com/rana_kualu/items/a6601b49e0591eb42200) PHP 7.4.x から PHP 8.0.x への移行(https://www.php.net/migration80)
ご清聴ありがとうございました! 今日の内容の詳細や追加 情報を記事にしています! (右のQRまたは https://qiita.com/SanQ/ite ms/f8453d6a7cbac6852312)