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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Masaru Yamagishi
September 24, 2022
Programming
950
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
130
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
1k
3D ブラウザゲーム開発を始めたい人のために準備している話
myamagishi
1
220
「DI」と仲良くなる
myamagishi
6
3.6k
大解剖!amphpを使って非同期 PHP を実現しよう!
myamagishi
1
4k
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
Cache-moi si tu peux : patterns et pièges du cache en production - Devoxx France 2026 - Conférence
slecache
0
160
iOS機能開発のAI環境と起きた変化
ryunakayama
0
180
Feature Toggle は捨てやすく使おう
gennei
0
570
煩雑なSkills管理をSoC(関心の分離)により解決する――関心を分離し、プロンプトを部品として育てるためのOSSを作った話 / Solving Complex Skills Management Through SoC (Separation of Concerns)
nrslib
4
870
夢の無限スパゲッティ製造機 -実装篇- #phpstudy
o0h
PRO
0
200
「速くなった気がする」をデータで疑う
senleaf24
0
170
一度始めたらやめられない開発効率向上術 / Findy あなたのdotfilesを教えて!
k0kubun
4
2.9k
「効かない!」依存性注入(DI)を活用したAPI Platformのエラーハンドリング奮闘記
mkmk884
0
320
Don't Prompt Harder, Structure Better
kitasuke
0
710
3分でわかるatama plusのQA/about atama plus QA
atamaplus
0
150
ハーネスエンジニアリングにどう向き合うか 〜ルールファイルを超えて開発プロセスを設計する〜 / How to approach harness engineering
rkaga
11
4.4k
アーキテクチャモダナイゼーションとは何か
nwiizo
17
4.9k
Featured
See All Featured
Amusing Abliteration
ianozsvald
1
150
How to Talk to Developers About Accessibility
jct
2
170
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
210
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
Testing 201, or: Great Expectations
jmmastey
46
8.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.1k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.2k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
250
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.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 まとめ