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
Reflection を使いこなして、 オブジェクトを型安全に マッピングしよう!
Search
Masaru Yamagishi
September 24, 2022
Programming
1k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Reflection を使いこなして、 オブジェクトを型安全に マッピングしよう!
PHP Conference Japan 2022 LT Slide
Masaru Yamagishi
September 24, 2022
More Decks by Masaru Yamagishi
See All by Masaru Yamagishi
Babylon.js 勉強会 vol.4 JAPAN 活動紹介
myamagishi
0
180
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
1.1k
3D ブラウザゲーム開発を始めたい人のために準備している話
myamagishi
1
250
「DI」と仲良くなる
myamagishi
6
3.8k
大解剖!amphpを使って非同期 PHP を実現しよう!
myamagishi
1
4.2k
PHP で負荷試験のシナリオを書きたい!ので amphp を使って自作した件
myamagishi
2
1.3k
秒間 10,000 リクエストを "簡単に"いなすゲームサーバーを Laravel で作る設計
myamagishi
19
17k
xR グラスが普及した新時代を妄想する - XRKaigi 2022
myamagishi
0
320
世界最速? で PHP8 Native Framework 作った
myamagishi
1
870
Other Decks in Programming
See All in Programming
Intent as Code
shoppingjaws
6
1.1k
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
280
iOSDC Japan 2026 - Swiftで作って学ぼう!データベース自作入門
kaseken
2
410
GitHubハンズオン講座 — 実務レベルのチーム開発のフローを身につけよう
junhat6
0
110
Security issues being discussed on Web Platforms
petamoriken
0
1.4k
Verilogで学ぶCPU自作入門.pdf
uyuki234
7
3.8k
パズルゲームの作り方 / how to make puzzle games
kaityo256
PRO
2
250
Heart of Swift Concurrency
koher
0
1.1k
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
30
13k
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
770
The Rails Doctrine Decade
koic
2
420
すこし踏み込む CancellationToken
htkym
2
1.5k
Featured
See All Featured
Odyssey Design
rkendrick25
PRO
2
840
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
330
It's Worth the Effort
3n
188
29k
Become a Pro
speakerdeck
PRO
31
6.3k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
380
[RailsConf 2023] Rails as a piece of cake
palkan
59
7k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
550
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
The Pragmatic Product Professional
lauravandoore
37
7.5k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.3k
Transcript
Reflection を使いこなして、 オブジェクトを型安全に マッピングしよう! PHP Conference Japan 2022 - やまゆ
https://fortee.jp/phpcon-2022/proposal/51f66d0e-5bae-4902-bb63-89700407204a
やまゆ - 札幌の PHPer(㈱インフィニットループ) - Babylon.js 勉強会 - 好き: 🍶、☕、🐇、デレマス、カピバラ
- 最近: 壱百満天原サロメ、スプラトゥーン3
None
何故 LT 枠で 応募した自分?
Reflection
Reflection とは? - ランタイム等で「コードそのもの」の構造を取得・改変する技術 - 引数の型や名前を取得したり、 private メソッドを無理やり外で呼べる (危険) -
メタプログラミングとも呼ばれる - ビジネスロジックでは使われることはほぼないが、ライブラリやFWでは重要 - PHP 8 Attribute も Reflection 経由で取得できる - PHP は(大雑把に言うと)コンパイルしないので、 容易に Reflection 情報を取得できる
例) json をクラスに変換したい
例) json をクラスに変換したい
こういう IF だと良さそう
こういう IF だと良さそう psalm でジェネリクス型を疑似表現! クラス名文字列とIDを指定して変換
実装例
実装例
実装例 該当クラスのメタ情報を取得 コンストラクタパラメータ一覧取得 null は優先して処理 ここでインスタンス化
実装例
- PHP では簡単に Reflection を扱える - PHP 8.1~ の型に優しい実装 -
データのマッピングだけでなく、コード自動生成などにも寄与する - 社内ライブラリとして共有されていることもありそう - ビジネスロジックを楽にする仕組みとして入れると良い - Reflection は いいぞ。 https://gist.github.com/il-m-yamagishi/b845406086fdef32a5f794ff02eba30a Reflection まとめ