Generic types
Generic types allow us to reuse the same code for different types.
The implementation of generic types is usually based on a template.
For a given type or class, we generate a T
template, which can be used safely further.
Generic types do not exist in PHP. In 2016 this RFC was created, but at this moment it is not known if and when it will be implemented.
Munus uses docblock from PHPStan or Psalm. We can use this tools to analyse our code for type safe.
In the following example, we will declare a template type T for Value
class, and use it in get
method as return param:
Now Value
is our generic wrapper for any kind of type. For example we can create Value
of int
, string
or even DateTime
type.
Each Value
in Munus is generic.