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
350
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PHP8で導入されるmatch式 / php8_match
PHP8で導入されるmatch式についての発表した内容です。
kuwacchi
September 25, 2020
More Decks by kuwacchi
See All by kuwacchi
おすすめの技術書 LT会_リファクタリング(第2版)/LT_refactor
kuwacchi
0
22k
スモークE2Eテストで20年モノのシステムからバグを燻し出す方法 / smokeE2E
kuwacchi
0
2.2k
Other Decks in Technology
See All in Technology
老害フォレンジッカーはAI羊の夢を見るか?
tadmaddad
0
340
Apache Icebergインフラストラクチャ:ストレージ・カタログ・エンジンの選択肢とClouderaプラットフォームでの実装
tsugiyama
0
110
My broken English still works: speaking at global OSS events
naruoga
0
110
QAエンジニア起点で進める、SmartHRにおける信頼性向上について
kaomi_wombat
1
150
モダンフロントエンド 開発研修
recruitengineers
PRO
4
730
[ChatGPT Work LT]事務作業が苦手な人のための バックオフィスの「半」自動化
chimaki_iot
0
310
AWS ネットワーク構築でハマった(ハマりかけた) 5選とそこから得た教訓
nagisa53
4
240
Agent 時代の Kaggle 展望 / kaggle-in-the-agentic-era
upura
1
780
ボトムアップ文化が強い組織で セキュリティをどう根付かせていくかの現在進行形の話 / Making Security Stick in a Bottom-Up Organization
yamaguchitk333
0
240
修正PRを食べてレビュースキルが賢くなる:Claude Codeによる自己改善サイクル
yuyaumetsu
6
1.6k
MIRU 2026 チュートリアル
keisuke198619
0
960
ハーレムエンジニアリング
kazuma777777
0
180
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
340
New Earth Scene 8
popppiees
3
2.5k
YesSQL, Process and Tooling at Scale
rocio
174
15k
Being A Developer After 40
akosma
91
590k
GitHub's CSS Performance
jonrohan
1033
470k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
170
Git: the NoSQL Database
bkeepers
PRO
432
67k
Design in an AI World
tapps
1
280
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. ご清聴ありがとうございました
桑原 健悟