|
|
|
PHP Type Alias
|
|
3 points
by oladoyinbov
979 days ago
|
|
i was kinda wondering maybe it's worth it to write an RFC on PHP type alias. this will be really useful in the sense of maintenance, re-usability, readability and many more.. ```
<?php type T = bool|int; type Result = string; function greeting(T $input): Result {
return match() { true, 1 => 'hello user :) ',
false, 2 => 'good-bye user'
}
}``` What's your opinion on this? |
|
In contrast to TypeScript or Flow, which can only check the program flow at compile time, PHP will crash and burn if you pass an int into a string function parameter at runtime.
Would you expect the presumed type aliases to be checked at runtime too?
There are already union types in PHP 8, but no aliases.
I think in PHP it is always the best approach to work with classes, whose type you can check at runtime (in contrast to duck-typed languages such as JS, where there is instanceof but that is generally a last resort).
Structural types checked at compile time are a different approach to the one PHP chose I think. Alhough the lines between compilation and execution are blurry in a JIT language of course.