Generic Programming: Instead of writing a separate sortNumbers, sortStrings, sortUsers, you write one generic sort that the compiler specialises (or checks) for whatever type T you use it with. Generic programming means writing code once for many types. A generic sort function works for numbers, strings and users alike, instead of having a separate version for each.
Frequently asked questions about Generic Programming
How is Generic Programming used in practice?
The type is a parameter, usually written as T. A Collection<User> and a Collection<Order> share all their code, while the compiler still knows exactly what each one contains.
What should you keep in mind with Generic Programming?
The benefit is reuse without losing type safety. You get autocompletion and errors at compile time, rather than surprises at runtime. TypeScript, Java, C#, Go and Rust support generics natively. In PHP, the same effect comes from docblock annotations checked by PHPStan or Psalm. Laravel's own code uses them extensively, which is why your editor knows what User::query()->first() returns.
Implement proven backend architecture patterns including Clean Architecture, Hexagonal Architecture, and Domain-Driven Design. Use this skill when designing clean architecture for a new microservice, when refactoring a monolith to use bounded contexts, when implementing hexagonal or onion architecture patterns, or when debugging dependency cycles between application layers.