The most significant features of PHP 7 besides performance boosts is the improved type system.
It took until PHP 8.0 before most key features were implemented. Overall, PHP’s type system has improved significantly over the past few years. The system is maturing and some community projects started to use types to their full extent.
Let’s take a look back in the pats and see the differences between PHP5 and PHP8 Type System.
Firstly, more built-in types were added, they are called “scalar” types. These types are integers, string, boolean and floats. Return types were added so that before PHP 7 you were able to use the type hint only for input parameters, this leading to a mix of doc block types and inline types.
It was a mess
It is convoluted, ugly and hard to read at times.
Some developers who didn’t want to deal with this choose not to use types at all. After all the level of safety was limited since doc blocks aren’t interpreted AT ALL Only your IDE would be able to understand the doc block version.
As of PHP 7.00, the previous example could be rewritten like this:
It is the same information but much more concise and ACTUALLY CHECKED by the interpreter.
Besides the parameters and return types, now there are also typed properties for classes! Just like the parameters and return types, they are optional; PHP won’t do any type checks.
Both scalar types and objects are allowed. Now, typed properties have an interesting features. Look at this example and notice that this is valid and does not throw errors: