CHAPTER 18

image

Type Hinting

PHP relies on the proper documentation of functions for developers to know what arguments a function can take. To simplify this PHP 5 introduced type hinting, which allows a function to specify the type of arguments it accepts. Allowed types include classes, interfaces and the pseudo types array and callable.

Name Description
class name Argument must be an object of this class or a child.
interface name Argument must be an object implementing this interface.
array Argument must be an array.
callable Argument must be callable as a function.

A type hint is set by prefixing the parameter with ...

Get PHP Quick Scripting Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.