is there any advantage to providing types for arguments:
public static function DoStuff( string $str ) {
echo $str;
}
in comparison to not using them?
public static function DoStuff( $str ) {
echo $str;
}
Could it possibly run faster if I declare my types?
Yes, it matters. The arguments must be given in the order the function expects them.
To add a type hint to a parameter, you place a type in front of it like this: <? php function my_function(type $param1, type param2, ...) { // ... }
Parameters are temporary variable names within functions. The argument can be thought of as the value that is assigned to that temporary variable.
PHP native functions According to the manual, PHP functions may accept up to 12 arguments.
You cannot (with current versions of PHP) specify scalar types, such as string or int. You can only specify a class/interface, or array.
Relevant page of the manual: Type Hinting.
I see at least one great advantage of using type hinting: readability. It's easier to see what kind of parameter a function/method expects.
Some will see this as a drawback that if you don't respect a type hint and pass some not-type-valid parameter, you'll get a catchable fatal error. However,
If your question is about speed:
In conclusion, a personal opinion: I quite often use those type hints, just so:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With