Use Generics in PHP Today
summary
Shows how to get true generics in PHP today using PHPDoc generics and static analyzers like PHPStan and Psalm, not language changes. Demonstrates concrete patterns: function and class templates, making ReflectionClass and builtins generic with stop files, typing collections and inferring return types, coupling original→derivative types, and newer call-site variance and star projection features that lift common limitations so static analysis reports correct errors instead of silencing real bugs.
AI-written summary · about
chapters (25)
- 0:00Introduction
- 0:43PHPStan and generics
- 1:51Understanding generics
- 3:39Generics in static analysis
- 5:05PHP lacks native generics
- 6:08Type variables syntax
- 7:14Generics for functions
- 8:18Real-world array_map
- 10:25Generic classes
- 12:55Extending generic classes
- 14:45PHPStan errors with generics
- 15:32ReflectionClass type issues
- 18:06Generic interfaces
- 19:57PHPStan stub files
- 21:44Type safety with generics
- 25:17Coupling classes
- 27:05Debugging PHPStan
- 29:17Generics limitations
- 30:19Covariance contravariance
- 33:20Collection type issues
- 36:16Call-site variance
- 39:10Contravariant declarations
- 40:14Star projection
- 41:45Learning resources
- 43:07Q&A








