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
2
670
Reflection を使いこなして、 オブジェクトを型安全に マッピングしよう!
PHP Conference Japan 2022 LT Slide
Masaru Yamagishi
September 24, 2022
Tweet
Share
More Decks by Masaru Yamagishi
See All by Masaru Yamagishi
3D ブラウザゲーム開発を始めたい人のために準備している話
myamagishi
1
79
「DI」と仲良くなる
myamagishi
5
2.5k
大解剖!amphpを使って非同期 PHP を実現しよう!
myamagishi
1
2.4k
PHP で負荷試験のシナリオを書きたい!ので amphp を使って自作した件
myamagishi
2
640
秒間 10,000 リクエストを "簡単に"いなすゲームサーバーを Laravel で作る設計
myamagishi
9
12k
xR グラスが普及した新時代を妄想する - XRKaigi 2022
myamagishi
0
150
世界最速? で PHP8 Native Framework 作った
myamagishi
1
730
Eloquent Driven Development
myamagishi
2
730
Laravel x nuxt.js ~認証難しい問題~
myamagishi
0
990
Other Decks in Programming
See All in Programming
Prolog入門
qnighy
4
990
RAGの回答精度評価用のQAデータセットを生成AIに作らせた話
kurahara
0
240
Using Livebook to build and deploy internal tools @ ElixirConf 2024
hugobarauna
0
240
ドメイン駆動設計を実践するために必要なもの
bikisuke
3
330
Go Code Generation at newmo / 2024-08-27 #newmo_layerx_go
genkey6
0
550
長期運用プロダクトの開発速度を維持し続けるためのリファクタリング実践例
wataruss
8
2.7k
Understand the mechanism! Let's do screenshots tests of Compose Previews with various variations / 仕組みから理解する!Composeプレビューを様々なバリエーションでスクリーンショットテストしよう
sumio
3
480
XStateでReactに秩序を与えたい
gizm000
0
710
Prompt Cachingは本当に効果的なのか検証してみた.pdf
ttnyt8701
0
530
実践!難読化ガイド
mitchan
0
120
rbs-inlineを導入してYARDからRBSに移行する
euglena1215
1
260
Swiftコードバトル必勝法
toshi0383
0
150
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
304
110k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Learning to Love Humans: Emotional Interface Design
aarron
270
40k
Faster Mobile Websites
deanohume
304
30k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
36
1.7k
The Brand Is Dead. Long Live the Brand.
mthomps
53
37k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
663
120k
Teambox: Starting and Learning
jrom
131
8.7k
What's in a price? How to price your products and services
michaelherold
242
11k
Building Better People: How to give real-time feedback that sticks.
wjessup
359
18k
Design by the Numbers
sachag
277
19k
Raft: Consensus for Rubyists
vanstee
135
6.5k
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 まとめ