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
PHPにおけるアトリビュートの活用方法
Search
will1992114
November 30, 2022
Programming
0
380
PHPにおけるアトリビュートの活用方法
PHPerのための「PHPフレームワーク」を語り合うPHP TechCafe
で発表
https://rakus.connpass.com/event/264108/
will1992114
November 30, 2022
Tweet
Share
Other Decks in Programming
See All in Programming
スキーマ駆動開発による品質とスピードの両立 - 私達は何故、スキーマを書くのか
kentaroutakeda
0
170
データアナリストが行うDatabricksを活用したETLの自動化事例
shinoa
0
270
OpenAPIを中心に考えるAPI開発入門 / Introduction to API Development with a Focus on OpenAPI
seike460
PRO
2
170
R言語の環境構築と基礎 Tokyo.R 112
bob3bob3
0
270
検証も兼ねて個人開発でHonoとかと向き合った話
hanetsuki
0
890
Elm Form Validation
bkuhlmann
0
510
try! Swift Tokyo 初参加報告LT
hinakko2
0
220
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
360
StoreKit2によるiOSのアプリ内課金のリニューアル
kangnux
0
110
Behind VS Code Extensions for JavaScript / TypeScript Linnting and Formatting
unvalley
5
910
効率化に挑戦してみたらモバイル開発が少し快適になった話
ryunakayama
0
130
Zero Waste, Radical Magic, and Italian Graft – Quarkus Efficiency Secrets
hollycummins
0
230
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
345
19k
The Mythical Team-Month
searls
216
42k
StorybookのUI Testing Handbookを読んだ
zakiyama
13
4.6k
How GitHub (no longer) Works
holman
304
140k
Why Our Code Smells
bkeepers
PRO
331
56k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
60
14k
How to Ace a Technical Interview
jacobian
272
22k
Build your cross-platform service in a week with App Engine
jlugia
225
17k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
221
21k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
Fantastic passwords and where to find them - at NoRuKo
philnash
37
2.5k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
125
32k
Transcript
PHPにおけるアトリ ビュートの活⽤⽅法 株式会社ケアリッツ・テクノロジーズ 栗原 駿 2022/11/29
⾃⼰紹介 • 名前:栗原 駿 • 所属:株式会社ケアリッツ・テクノロジーズ • 主な経験⾔語:C# • PHPは業務で使い始めて半年ほどです。
• 趣味:マラソン(最近全然⾛ってない…) • 最近はサウナにはまってます!
話すこと • PHPにおけるアトリビュートとは • 活⽤例①:バリデーションの共通化 • 活⽤例②:ロギング処理の共通化
PHPにおけるアトリビュートとは #[Description("アトリビュートの説明⽤のクラス")] class Example { #[Description("プロパティ")] public string $key; #[Description("メソッド")]
public function execute( #[Description("引数")]$arg) : void { } }
PHPにおけるアトリビュートとは • メタデータをコードの宣⾔時に埋め込むことができる • 実⾏時にリフレクションAPIで読み取って使⽤する • 様々な⽤途で使⽤できる • ロジックの共通化 •
IDEへの補助的な情報の通知 • クラス、メソッド、関数、パラメータ、プロパティ、クラス定数に指定できる
<?php class SampleClass { public $keyA; public $keyB; public function
__construct($keya, $keyb) { $this->keyA = $keya; $this->keyB = $keyb; if($this->keyA === null) { throw new Exception($property->getName() . "は必須のプロパティです。"); } } } • 活⽤法①:バリデーションの共通化 • コンストラクタ実⾏時のバリデーションをアトリビュートで共通化する。
活⽤法①:バリデーションの共通化 <?php class SampleClass { use Validator; #[Required] public $keyA;
public $keyB; public function __construct($keya, $keyb) { $this->keyA = $keya; $this->keyB = $keyb; $this->validate($this); }
活⽤法①:バリデーションの共通化 <?php trait Validator { function validate($input): bool { $reflection
= new ReflectionObject($input); foreach ($reflection->getProperties() as $key => $property) { $isRequired = $property->getAttributes(Required::class) > 0; if ($isRequired && $property->getValue($input) === null) { throw new Exception($property->getName() . "は必須のプロパティです。"); } }
活⽤例②:ロギング処理の共通化 • アトリビュートを付加したメソッドについて、 実⾏開始時と完了時にログを出⼒する • Ray.Aopというフレームワークを使⽤する • AOP(アスペクト指向プログラミング)のための機能を提供する
活⽤例②:ロギング処理の共通化 LogSample (処理を注⼊したいクラス) Log (処理の注⼊先を指定するア トリビュート) Ray.Aop LogInterceptor (注⼊する処理の実装) 特定の関数に指定
#[Log]を指定するクラスに対して、 インスタンス化時にLogInterceptor を注⼊する
活⽤例②:ロギング処理の共通化 #[Attribute(Attribute::TARGET_METHOD)] class Log { } class LogSample { #[Log]
public function LogSample($arg1, $arg2) { echo ("LogSample() を実⾏しています" . "¥n"); return "LogSample()のReturn"; }
活⽤例②:ロギング処理の共通化 class LogInterceptor implements MethodInterceptor { public function invoke(MethodInvocation $invocation)
{ // 処理実⾏前 echo (date(DATE_ATOM) . ":" . $invocation->getMethod()->getName() . "の実⾏前 引数:" . json_encode($invocation->getArguments()) . "¥n"); // 実処理の実⾏ $result = $invocation->proceed(); // 処理実⾏後 echo (date(DATE_ATOM) . ":" . $invocation->getMethod()->getName() . "の実⾏後 戻り値:" . $result . "¥n"); }
活⽤例②:ロギング処理の共通化 $pointcut = new Pointcut( (new Matcher())->any(), (new Matcher())->annotatedWith(Log::class), [new
LogInterceptor()] ); $bind = (new Bind())->bind(LogSample::class, [$pointcut]); $tmpDir = __DIR__ . '/tmp'; $sample = (new Weaver($bind, $tmpDir))->newInstance(LogSample::class, []); $sample->LogSample("arg1", "arg2");
活⽤例②:ロギング処理の共通化 実⾏結果… > 2022-11-28T22:01:17+00:00:LogSampleの実⾏前 引数:{“0”:“arg1”,“1”:“arg2”} ←LogInterceptorのLog(実⾏前) > LogSample() を実⾏しています ←LogSampleのLog
> 2022-11-28T22:01:17+00:00:LogSampleの実⾏後 戻り値:LogSample()のReturn ←LogInterceptorのLog(実⾏後)
まとめ • PHPのアトリビュートを使うことでコードにメタデータを付加できる • 付加したメタデータを実⾏時に読み取ることでロジックの共通化ができる
ご清聴ありがとうございました