Type hinting

suggest change

Syntax

Remarks

Type hinting or type declarations are a defensive programming practice that ensures a function’s parameters are of a specified type. This is particularly useful when type hinting for an interface because it allows the function to guarantee that a provided parameter will have the same methods as are required in the interface.

Passing the incorrect type to a type hinted function will lead to a fatal error. On PHP 8 the uncaught message looks like this:

Fatal error: Uncaught TypeError: foo(): Argument #1 ($param) must be of type RequiredType, ProvidedType given

PHP 7 worded the same error as Argument 1 passed to foo() must be of the type RequiredType, ProvidedType given.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents