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
570
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
Result型で“失敗”を型にするPHPコードの書き方
kajitack
4
260
Effect の双対、Coeffect
yukikurage
5
1.4k
Webからモバイルへ Vue.js × Capacitor 活用事例
naokihaba
0
760
Benchmark
sysong
0
250
なぜ適用するか、移行して理解するClean Architecture 〜構造を超えて設計を継承する〜 / Why Apply, Migrate and Understand Clean Architecture - Inherit Design Beyond Structure
seike460
PRO
1
660
5つのアンチパターンから学ぶLT設計
narihara
1
110
関数型まつり2025登壇資料「関数プログラミングと再帰」
taisontsukada
2
850
生成AIコーディングとの向き合い方、AIと共創するという考え方 / How to deal with generative AI coding and the concept of co-creating with AI
seike460
PRO
1
330
Composerが「依存解決」のためにどんな工夫をしているか #phpcon
o0h
PRO
1
210
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
2
230
たった 1 枚の PHP ファイルで実装する MCP サーバ / MCP Server with Vanilla PHP
okashoi
1
170
生成AIで日々のエラー調査を進めたい
yuyaabo
0
640
Featured
See All Featured
A Tale of Four Properties
chriscoyier
160
23k
Product Roadmaps are Hard
iamctodd
PRO
53
11k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
670
Art, The Web, and Tiny UX
lynnandtonic
299
21k
Six Lessons from altMBA
skipperchong
28
3.8k
GraphQLとの向き合い方2022年版
quramy
47
14k
Testing 201, or: Great Expectations
jmmastey
42
7.5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
33
5.9k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
The Invisible Side of Design
smashingmag
299
51k
Producing Creativity
orderedlist
PRO
346
40k
It's Worth the Effort
3n
185
28k
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のアトリビュートを使うことでコードにメタデータを付加できる • 付加したメタデータを実⾏時に読み取ることでロジックの共通化ができる
ご清聴ありがとうございました