Webアプリケーション ⼊⼒:HTTPリクエスト POST /path HTTP/1.1 Host: example.com Content-Type: application/x-www-form-urlencoded this=is&request=body 出⼒:HTTPレスポンス HTTP/1.1 200 OK Host: example.com Content-Type: text/plain;charset=UTF-8 X-Powered-By: PHP/8.0.0 This is response body Nextat Inc. 8
After class PostController { public function __construct(PostService $service) {} public function show(int $postId): JsonResponse { $post = $this->service->find($postId); // 略 } } Nextat Inc. 35
⼩クラス主義の難点 AがBに依存していて、BがCに依存していて、CがDに依存していて…… ⼈の⼿で⽣成コードを利⽤するすべての箇所で書くのは⼤変 $handler = new GreetingHandler( new GreetingService( new RdbGreetingRepository( new DatabaseConnection( new Pdo($dsn) ) ) ) ); Nextat Inc. 37
Attributesのシンタックス #[ ORM\Entity, ORM\Table("user") ] class User { #[ORM\Id, ORM\Column("integer"), ORM\GeneratedValue] private $id; #[ORM\Column("string", ORM\Column::UNIQUE)] #[Assert\Email(["message" => "The email '{{ value }}' is not a valid email."])] private $email; } https://wiki.php.net/rfc/shorter_attribute_syntax_change Nextat Inc. 41
Web FWへのAttributes活⽤例 デコレータ/アノテーションを機能として持つ⾔語界隈でよく⾒る例 ルートの定義をリクエストハンドラのすぐ側で⾏う リクエストパラメータやレスポンスの暗黙的な変換を明⽰的に⽰すDSL #[RequireLoginMiddleware] class ShowPostHandler { #[Route\Get('posts/{postId}')] #[JsonResponse] #[Cache] public function handle( #[RouteParam] int $postId ): array { return Post::find($postId)->toArray(); } } Nextat Inc. 43
3-3. Union Typesなど型に関する機能 Union Types Static return type (PHP7.4ですが) Typed Properties 静的解析ブーム IDEの静的解析の強化 PHPStan, Psalmなど静的解析ツールの進化 静的解析フレンドリーなFWも増えるはず Nextat Inc. 45
protobuf-net.Grpcの例(アノテーションを活⽤) [DataContract] public class User { [DataMember(Order = 1)] public uint id { get; set; } [DataMember(Order = 2)] public string name { get; set; } } 対応するgRPCのメッセージ message User { uint32 id = 1; string name = 2; } Nextat Inc. 62
メッセージ駆動、マイクロサービスについて詳しく学ぶ → 事業のスケールアウトを⽀えるPHPで作る分散アーキテクチャ https://speakerdeck.com/ytake/shi-ye-falsesukeruautowozhi-eru-phpdezuo- rufen-san-akitekutiya → Service communication re:Born Nextat Inc. 68