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
Reflection を使いこなして、 オブジェクトを型安全に マッピングしよう!
Search
Masaru Yamagishi
September 24, 2022
Programming
960
2
Share
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
140
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
1k
3D ブラウザゲーム開発を始めたい人のために準備している話
myamagishi
1
220
「DI」と仲良くなる
myamagishi
6
3.6k
大解剖!amphpを使って非同期 PHP を実現しよう!
myamagishi
1
4.1k
PHP で負荷試験のシナリオを書きたい!ので amphp を使って自作した件
myamagishi
2
1.2k
秒間 10,000 リクエストを "簡単に"いなすゲームサーバーを Laravel で作る設計
myamagishi
19
16k
xR グラスが普及した新時代を妄想する - XRKaigi 2022
myamagishi
0
300
世界最速? で PHP8 Native Framework 作った
myamagishi
1
860
Other Decks in Programming
See All in Programming
[RubyKaigi 2026] Require Hooks
palkan
1
280
Back to the roots of date
jinroq
0
620
HTML-Aware ERB: The Path to Reactive Rendering @ RubyKaigi 2026, Hakodate, Japan
marcoroth
0
600
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
310
2026_04_15_量子計算をパズルとして解く
hideakitakechi
0
130
Claude CodeでETLジョブ実行テストを自動化してみた
yoshikikasama
0
1.1k
CDK Deployのための ”反響定位”
watany
5
920
YJITとZJITにはイカなる違いがあるのか?
nakiym
0
440
How We Benchmarked Quarkus: Patterns and anti-patterns
hollycummins
1
180
Swift Concurrency Type System
inamiy
1
570
How We Practice Exploratory Testing in Iterative Development( #scrumniigata ) / 反復開発の中で、探索的テストをどう実施しているか
teyamagu
PRO
3
550
PicoRuby for IoT: Connecting to the Cloud with MQTT
yuuu
2
720
Featured
See All Featured
Statistics for Hackers
jakevdp
799
230k
Become a Pro
speakerdeck
PRO
31
5.9k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
360
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.3k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
190
Designing for humans not robots
tammielis
254
26k
Un-Boring Meetings
codingconduct
0
280
Into the Great Unknown - MozCon
thekraken
41
2.4k
The Limits of Empathy - UXLibs8
cassininazir
1
320
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
130
First, design no harm
axbom
PRO
2
1.2k
The untapped power of vector embeddings
frankvandijk
2
1.7k
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 まとめ