June 2017
Intermediate to advanced
536 pages
9h 49m
English
Type hinting features are not limited to function parameters only; as of PHP 7, they expand to function return values as well. The same rules that apply to function parameters hinting, apply to function return type hinting. To specify a function return type, we simply follow the parameter list with a colon and the return type, as shown in the following example:
function register(string $user, int $age) : bool { // logic ... return true;}
Developers can still write functions with multiple conditioned return statements; its just that in this case, each of these return statements, when reached, will have to match the hinted return type, otherwise \TypeError will be thrown.
The function return type hints play nicely with super ...
Read now
Unlock full access