Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is Type Hinting "in php"?

user-image
Question added by Adel Ezat Fawzy Ellozy , Webdeveloper. , Saudi Arabian Maritiem Sports Federation
Date Posted: 2017/04/09
Ahmad Saadaldeen
by Ahmad Saadaldeen , OOP PHP Developer , Enbit GmbH (http://enbit.de/)

Type declarations (or type hints) allow functions to require that parameters are of a certain type at call time. If the given value is of the incorrect type, then an error is generated: in PHP 5, this will be a recoverable fatal error, while PHP 7 will throw a TypeError exception.

 

To specify a type declaration, the type name should be added before the parameter name. The declaration can be made to accept NULL values if the default value of the parameter is set to NULL.

 

source: http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

Parvez Ahmed
by Parvez Ahmed , Frontend Developer (Angular) , GREEN DELTA INSURANCE COMPANY LIMITED

Type hinting means that whatever you pass must be an instance of (the same type as) the type you're hinting.

"Type hinting" forces you to only pass objects of a particular type. This prevents you from passing incompatible values, and creates a standard if you're working with a team etc.

alaa liswe
by alaa liswe , ِAdministrative Assistant , Arab Open University

Since PHP 5 you can use type hinting to specify the expected data type of an argument in a function declaration. ... By telling PHP exactly what kind of objects the enroll() method expects to receive, you can ensure that students are being enrolled in a school instead of a nunnery or a 401K.

medhatmahmoud mahmoud
by medhatmahmoud mahmoud , Web Designer , Connect Computer Service Co

you can use type hinting to specify the expected data type of an argument in a function declaration. When you call the function, php will check whether or not the arguments are of the specified type. If not, the run-time will raise an error and execution will be halted.

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.