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で導入されるmatch式 / php8_match
Search
kuwacchi
September 25, 2020
Technology
1
320
PHP8で導入されるmatch式 / php8_match
PHP8で導入されるmatch式についての発表した内容です。
kuwacchi
September 25, 2020
Tweet
Share
More Decks by kuwacchi
See All by kuwacchi
おすすめの技術書 LT会_リファクタリング(第2版)/LT_refactor
kuwacchi
0
21k
スモークE2Eテストで20年モノのシステムからバグを燻し出す方法 / smokeE2E
kuwacchi
0
1.8k
Other Decks in Technology
See All in Technology
CDK Vibe Coding Fes
tomoki10
1
530
Zero Data Loss Autonomous Recovery Service サービス概要
oracle4engineer
PRO
2
7.8k
Lakebaseを使ったAIエージェントを実装してみる
kameitomohiro
0
180
CDKTFについてざっくり理解する!!~CloudFormationからCDKTFへ変換するツールも作ってみた~
masakiokuda
1
200
shake-upを科学する
rsakata
7
930
TLSから見るSREの未来
atpons
2
240
American airlines ®️ USA Contact Numbers: Complete 2025 Support Guide
airhelpsupport
0
390
OpenTelemetryセマンティック規約の恩恵とMackerel APMにおける活用例 / SRE NEXT 2025
mackerelio
3
1.6k
AI エージェントと考え直すデータ基盤
na0
18
7.3k
[SRE NEXT] ARR150億円_エンジニア140名_27チーム_17プロダクトから始めるSLO.pdf
satos
5
2.1k
Getting to Know Your Legacy (System) with AI-Driven Software Archeology (WeAreDevelopers World Congress 2025)
feststelltaste
1
180
Delta airlines Customer®️ USA Contact Numbers: Complete 2025 Support Guide
deltahelp
0
1.1k
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
138
34k
Testing 201, or: Great Expectations
jmmastey
43
7.6k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.5k
Producing Creativity
orderedlist
PRO
346
40k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
Designing Experiences People Love
moore
142
24k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
700
Java REST API Framework Comparison - PWX 2021
mraible
31
8.7k
Music & Morning Musume
bryan
46
6.7k
RailsConf 2023
tenderlove
30
1.1k
Transcript
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. PHP8で導入される
match式 桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. 自己紹介 •氏名:桑原 健悟(くわはら けんご) •所属:株式会社ラクス
•お仕事:Mail Dealerの開発
#phpltjam ©2020 RAKUS Co., Ltd. 今日のお話 •https://tech-blog.rakus.co.jp/entry/20200917/php
#phpltjam ©2020 RAKUS Co., Ltd. 今日のお話 PHP8で導入されたmatch式について ・switchとの違いは? ・何が嬉しいの?
#phpltjam ©2020 RAKUS Co., Ltd. お品書き ・match式とは ・matchの特徴 ・値の代入が得意 ・さらばbreak忘れ
・条件忘れにエラーで気付ける ・厳密な比較
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. match式とは
桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. match式とは switchのような分岐の構文 $paramの値で処理を分岐
#phpltjam ©2020 RAKUS Co., Ltd. switchとmatchの違い match switch 返り値 あり
なし breakの記述 不要 必要 各条件の処理 1行でしか書けない ブロックで記述可能 比較 厳密(===) 緩やか(==) どの条件にも当てはまらない場合 エラーになる そのまま実行される
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. match式の特徴
桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. 値の代入が得意
桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. switchやifは「文」、matchは「式」 • 文とは • 値を返さない
→ 各条件で代入が必要 • 式とは • 値を返す → 代入忘れが発生しにくい 直接、値の代入はできない 値の代入は1回でいい
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. さらばbreak忘れ
桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. break忘れが起こり得ない • switch:明示的にbreakが必要 • match:breakが存在しない
→ break忘れが起こり得ない
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. 条件忘れにエラーで気付ける
桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. switch ユーザの成績(score)ごとに コマンド($command)を発行 日次処理でコマンドを実行するプログラム
#phpltjam ©2020 RAKUS Co., Ltd. match • matchは条件の網羅性を検査し該当がなければエラーとなる
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. 厳密な比較
桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. 厳密な比較 • 「数値だと思っていたら文字列であり思わぬ分岐に入った」がなくなる
#phpltjam ©2020 RAKUS Co., Ltd. 厳密な比較は常に嬉しい? • PHPの関数が厳密な型を意識した作りでないため、 思わぬバグとなる例を紹介
#phpltjam ©2020 RAKUS Co., Ltd. 厳密な比較は常に嬉しい? 結果:"jpgだよ" 結果:“defaultだよ"
#phpltjam ©2020 RAKUS Co., Ltd. 厳密な比較は常に嬉しい? preg_matchはtrueでなく、1を返す。 → match式ではtrue===1となり、該当してもケースに入らない
#phpltjam ©2020 RAKUS Co., Ltd. まとめ • match式はswitchに比べバグが入りにくい仕様 • 代入忘れしにくい
• break忘れがない • 条件忘れもエラーで気付ける • 厳密な比較は良い面も悪い面もある • 既存コードやPHPの関数の型を把握していないと思わぬバグに繋がる恐れも
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. ご清聴ありがとうございました
桑原 健悟