Explores four approaches to represent selectively loaded entity relations in PHP's type system using PHPStan. Starting from a simple ecommerce Product entity, the post evaluates: (1) separate classes per use-case, (2) interfaces with property hooks, (3) PHPStan generics with covariant template parameters, and (4) object shape

13m read timeFrom hbgl.dev
Post cover image
Table of contents
The Problem: Selectively loading relationsApproach 1: Class per use-caseApproach 2: InterfacesApproach 3: Generics all the wayApproach 4: Object shapes to the rescueSummary and outlook

Sort: