Benefits Of Type Declarations In PHP
Type declarations have been around in some form in PHP from PHP version 7, which was released in late 2015. So you can say that they have been there for a while now.
PHP 7 allowed scalar type declarations to be added to function arguments and return values. PHP 7.1 added support for nullable
parameter types and nullable
return types. Version 7.1 likewise added support for void
as a valid return type and a new compound type which was labelled the iterable
type, which is essentially an array or an object which can be traversed. Version 7.4 came with type declarations for class properties.
Let’s leave the history of type declarations in PHP at that and instead focus on how this feature of the language can serve to enhance the developer’s experience. This post will particularly focus on typed parameters, typed properties and return type declarations.
The documentation aspect
While this is an oversimplified example, it will serve the purpose. Any code editor with intellisense will allow the developer to know that the parameter required should be an integer. The documentation aspect becomes even more important with typed properties or parameters which are class types, some sort of service or helper class.
With typed parameters and typed properties, one can tell what type is expected when initializing the class or calling the function or method. Knowledge of what’s required brings us to the next point.
Error handling and debugging
Knowing what’s required from a function or a class constructor will prevent errors before hand as the code is self documenting to some extent. Peradventure, that an erroneous value is used, the error will get caught during runtime and the error will be clearer and thus easier to debug.
The same goes for return type declarations. Once again, with intellisense, the code editor will call you out for returning anything other than the required type.
Povilas Korop of Laravel Daily does an excellent job of explaining how return types can go a long way to easing the debugging process, as can be seen here.
Code completion with return type declaration
With the return type explicitly declared, the result of the method or function will be typed.
With intellisense code completion comes into play the moment $this->getBooks()
is typed. Whatever methods that’s available to the collection type/class will be suggested.
Conclusion
As seen above, utilizing type declarations can really enhance the development experience. PHP’s type system has enjoyed some more improvements with the release of versions 8 and 8.1.
For PHP 8 three new types were introduced. The union type, mixed pseudo type and static return type. Check out the improvements made to the type system in PHP 8 here.
PHP 8.1 brought along the never return type and intersection types. Support for Enumerated(Enum) types has also been included in this version, allowing developers to have types with a fixed number of possible values. This is all pretty interesting stuff which you can learn more about here.
If you aren’t utilizing type declarations in PHP, try it out and see the benefits that it will bring along.