Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
PHP8で導入されるmatch式 / php8_match
Search
kuwacchi
September 25, 2020
Technology
360
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.3k
Other Decks in Technology
See All in Technology
UniFi(ユニファイ)AIカメラの現場活用
ludicmind
1
180
1万名の社員が使う認証基盤で どう信頼性を担保するか?
kairim0
0
210
AI時代のAPI開発を加速する品質ガードレール / API Quality Guardrails in the AI Era
yokawasa
0
160
AI Made Us Faster at Solving the Wrong Problems
marceloancelmo
0
190
私の推しは「聞いてから進む」AIです -AI-DLCに一人でアプリを作らせた話
yama3133
1
180
Datadog の学び方 - あるいは、オブザーバビリティを学ぶとは何か
mananyuki
1
500
転んだ話が背中を預ける理由になる / A_story_about_falling_down_gives_you_a_reason_to_watch_each_other_s_backs.pdf
pauli
3
330
Snapshot Testing in Practice: Predictable and Reliable SwiftUI Views
fespinoza
0
140
全社共通データ基盤をつくる。ソニーのDatabricks活用とデータガバナンス設計の裏側
sony
0
430
子育てエンジニアの可処分時間が減っても成長を諦めない戦い方
sudoakiy
4
3.6k
生成AIを使って「人が」考える技術 ― AI時代の人機共想と実践ノウハウ|UNITT AC2026
ishiirikie
0
560
今こそ知りたいAmplifyGen2
mkdev10
2
160
Featured
See All Featured
Prompt Engineering for Job Search
mfonobong
0
490
The Limits of Empathy - UXLibs8
cassininazir
2
710
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
From π to Pie charts
rasagy
1
390
For a Future-Friendly Web
brad_frost
184
10k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
760
The SEO Collaboration Effect
kristinabergwall1
1
590
Rails Girls Zürich Keynote
gr2m
96
14k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.6k
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. ご清聴ありがとうございました
桑原 健悟