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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Shohei Okada
May 29, 2021
Programming
120
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
Transcript
24 slides
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 を使いたくなる • 聴いた人がこの発表で知った構文を実際に使う 本発表の趣旨(再掲)
More Decks by Shohei Okada
See All by Shohei Okada
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
440
たった 1 枚の PHP ファイルで実装する MCP サーバ / MCP Server with Vanilla PHP
okashoi
1
1.2k
どうして手を動かすよりもチーム内のコードレビューを優先するべきなのか
okashoi
3
2.5k
パスワードのハッシュ、ソルトってなに? - What is hash and salt for password?
okashoi
3
490
設計の考え方 - インターフェースと腐敗防止層編 #phpconfuk / Interface and Anti Corruption Layer
okashoi
11
6.8k
"config" ってなんだ? / What is "config"?
okashoi
0
2.1k
ファイル先頭の use の意味、説明できますか? 〜PHP の namespace と autoloading の関係を正しく理解しよう〜 / namespace and autoloading in php
okashoi
4
2.4k
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
Next.js: The final boss of Web Performance
guaca
0
400
Secure Your Coding Agent Like It’s Malware (v1.5) 🇬🇧 @Code.talks 2026
richargh
0
120
AI Augmentation Techniques with Liberty
ivargrimstad
1
440
GraphQL の onError と Nullability のこれから
quramy
0
210
느린 네트워크와 저사양 디바이스 사용자를 위한 웹뷰 로딩 시간 개선기
kmkim
1
220
複数ドメインを一つのアプリに統合するFlutterの設計と運用_FlutterKaigi2026登壇資料(須永高弘)
takahirosunaga
0
170
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
16
9k
SODA - FACT BOOK(EN)
sodainc
1
230
Distance field of Julia set
kinakomoti321
0
320
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
240
Performance Optimization With LLMs
xleitix
0
250
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Kaigi on Rails 2026, Tokyo, Japan
marcoroth
2
430
Featured
See All Featured
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Building an army of robots
kneath
307
47k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7k
Product Roadmaps are Hard
iamctodd
55
13k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
560
30 Presentation Tips
portentint
PRO
1
470
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
590
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
BBQ
matthewcrist
90
10k
Faster Mobile Websites
deanohume
310
32k
The Pragmatic Product Professional
lauravandoore
37
7.5k
KATA
mclloyd
PRO
35
16k