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
980
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
160
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
1.1k
3D ブラウザゲーム開発を始めたい人のために準備している話
myamagishi
1
240
「DI」と仲良くなる
myamagishi
6
3.7k
大解剖!amphpを使って非同期 PHP を実現しよう!
myamagishi
1
4.2k
PHP で負荷試験のシナリオを書きたい!ので amphp を使って自作した件
myamagishi
2
1.2k
秒間 10,000 リクエストを "簡単に"いなすゲームサーバーを Laravel で作る設計
myamagishi
19
16k
xR グラスが普及した新時代を妄想する - XRKaigi 2022
myamagishi
0
310
世界最速? で PHP8 Native Framework 作った
myamagishi
1
870
Other Decks in Programming
See All in Programming
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
540
数百円から始めるRuby電子工作
tarosay
0
140
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
350
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
490
AWS DevOps AgentのAzure接続機能を検証して見えた活用法/Use Cases Verified for the AWS DevOps Agent's Azure Connectivity Feature
masakiokuda
1
220
yield再入門 #phpcon
o0h
PRO
0
980
freee が目指す データ マネジメント戦略 AI-Ready 時代を支える 攻めのガバナンスとは
freee
PRO
0
300
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
160
Android CLI
fornewid
0
210
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
210
Built Our Own Background Agent at LayerX
layerx
PRO
9
5k
ビデオ通話が繋がる0.2秒で何が起きているのか
supurazako
2
180
Featured
See All Featured
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
780
Game over? The fight for quality and originality in the time of robots
wayneb77
1
240
Building the Perfect Custom Keyboard
takai
2
830
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
410
Become a Pro
speakerdeck
PRO
31
6.2k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
260
The Invisible Side of Design
smashingmag
301
52k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
270
First, design no harm
axbom
PRO
2
1.2k
Un-Boring Meetings
codingconduct
0
380
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 まとめ