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
PHP 8.0 の新記法を試してみよう!
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Shohei Okada
May 29, 2021
Programming
110
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PHP 8.0 の新記法を試してみよう!
PHP カンファレンス沖縄 2021
Shohei Okada
May 29, 2021
More Decks by Shohei Okada
See All by Shohei Okada
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
390
たった 1 枚の PHP ファイルで実装する MCP サーバ / MCP Server with Vanilla PHP
okashoi
1
1.1k
どうして手を動かすよりもチーム内のコードレビューを優先するべきなのか
okashoi
3
2.4k
パスワードのハッシュ、ソルトってなに? - What is hash and salt for password?
okashoi
3
450
設計の考え方 - インターフェースと腐敗防止層編 #phpconfuk / Interface and Anti Corruption Layer
okashoi
11
6.6k
"config" ってなんだ? / What is "config"?
okashoi
0
2k
ファイル先頭の use の意味、説明できますか? 〜PHP の namespace と autoloading の関係を正しく理解しよう〜 / namespace and autoloading in php
okashoi
4
2.3k
MySQL のインデックスの種類をおさらいしよう! / overviewing indexes in MySQL
okashoi
0
1.5k
PHP における静的解析(あるいはそもそも静的解析とは) / #phpcondo_yasai static analysis for PHP
okashoi
1
1.6k
Other Decks in Programming
See All in Programming
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.7k
モジュールの視点からSwiftを読み解く #iosdc
s_shimotori
0
300
SONY CISC-NEWS NWS-1750 + NWB-225 フレームバッファの NetBSD/news68k ドライバ実装 / OSC2026Hiroshima
tsutsui
0
170
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
130
Turning Architecture into Unit Tests in the AI Era (NSSpain XIV)
steliosf
PRO
1
120
Verilogで学ぶCPU自作入門.pdf
uyuki234
7
3.9k
カツオ、ご期待ください
suneo3476
0
140
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
330
AI Agent時代のリアーキテクチャ戦略と実践
hokaccha
9
5.3k
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1.2k
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
150
難しいけど、読めた。- OSSの入口に立った話。
sts11142
0
130
Featured
See All Featured
sira's awesome portfolio website redesign presentation
elsirapls
0
440
Navigating Team Friction
lara
192
16k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
2.9k
Faster Mobile Websites
deanohume
310
32k
How to Talk to Developers About Accessibility
jct
2
560
Chasing Engaging Ingredients in Design
codingconduct
0
340
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
540
The SEO Collaboration Effect
kristinabergwall1
1
580
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
930
Paper Plane
katiecoart
PRO
4
53k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Transcript
PHP 8.0 の新記法を 試してみよう! 2021/05/29 PHP カンファレンス沖縄 2021 @okashoi
岡田 正平/おかしょい Twitter: @okashoi GitHub: @okashoi 所属:株式会社ウィルゲート 登壇:
寄稿:
岡田 正平/おかしょい Twitter: @okashoi GitHub: @okashoi 所属:株式会社ウィルゲート 登壇:
寄稿: (バーチャルの姿)
2020/11/26
PHP 8.0 で追加された新しい構文や関数の中から すぐに使えそうなものをピックアップ、勢いにまかせて紹介 この発表のゴール • 聴いた人が PHP
8.0 を使いたくなる • 聴いた人がこの発表で知った構文を実際に使う 本発表の趣旨
本発表は『WEB+DB PRESS vol. 120』に寄稿した 「5 年ぶりの新版!PHP 8 登場」の内容をベースに 5 分間にぎゅっと詰め込んだものです
おことわり ※また、日本語訳は 私の独断によるものです
str_〇〇 関数
str_contains() 関数 <?php var_dump(str_contains('Hello, PHP8!', 'PHP')); var_dump(str_contains('Hello, PHP8!', 'PHP7')); /*
実行結果 bool(true) bool(false) */
str_starts_with(), str_ends_with() 関数 <?php var_dump(str_starts_with('Hello, PHP8!', 'Hello')); var_dump(str_starts_with('Hello, PHP8!', 'PHP8!'));
var_dump(str_ends_with('Hello, PHP8!', 'Hello')); var_dump(str_ends_with('Hello, PHP8!', 'PHP8!')); /* 実行結果 bool(true) bool(false) bool(false) bool(true) */
PHP 7 系以前では...... <?php // str_contains()相当 function my_str_contains(string $a, string
$b): bool { return strpos($a, $b) !== false; } // str_starts_with()相当 function my_str_starts_with(string $a, string $b): bool { return strpos($a, $b) === 0; } // str_ends_with()相当 function my_str_ends_with(string $a, string $b): bool { return strrpos($a, $b) === strlen($a) - strlen($b); }
null 安全演算子
null 安全演算子 ?-> <?php // $user が null の場合もエラーにならない $user?->changeName('okashoi');
// PHP 7 系以前では例えばこうしていた is_null($user) ?: $user->changeName('okashoi');
メソッドをチェインさせたいときに便利 <?php function example1(?Foo $foo): ?FooBar { if(is_null($foo)) { return
null; } $bar = $foo->bar(); if(is_null($bar)) { return null; } $buz = $bar->buz(); if(is_null($buz)) { return null; } return $buz->foobar(); } <?php function example2(?Foo $foo): ?FooBar { return $foo?->bar() ?->buz() ?->foobar(); }
match 式
match 式 <?php $english = match (1) { 1 =>
'one', 2 => 'two', 3 => 'three', default => 'more than three', }; var_dump($english); /* 実行結果 string(3) "one" */ ※ “match” は予約語に
1. 文(statement)ではなく式(expression)であること 2. break を書き忘れる心配がない 3. 厳密比較(===)が用いられる 4. 何にもマッチしないと UnhandledMatchError
を送出 5. 右辺には単一の式しか指定できない(※) ※ Future scope として「ブロック」を指定できるようにする旨の議論あり switch 文との違い
名前付き引数
名前付き引数 <?php function setFullName(string $givenName, string $familyName): void { //
省略 } // familyNameを先に指定できる setFullName(familyName: '岡田', givenName: '正平');
union 型
union 型 <?php // 引数にint型の値もfloat型の値も渡せる // 戻り値はint型またはfloat型のどちらか function square(int|float $x):
int|float { return $x * $x; }
mixed 型
mixed 型 <?php function foo(mixed $value): mixed { return $value;
} array|bool|callable|int|float|null|object|resource|string と同じ
もっと知りたい方は公式ドキュメントをチェック! https://www.php.net/releases/8.0/ WEB+DB PRESS の記事(バックナンバー)もヨロシク まだまだたくさん新機能!
PHP 8.0 で追加された新しい構文や関数の中から すぐに使えそうなものをピックアップ、勢いにまかせて紹介 この発表のゴール • 聴いた人が PHP
8.0 を使いたくなる • 聴いた人がこの発表で知った構文を実際に使う 本発表の趣旨(再掲)