Join David Powers for an in-depth discussion in this video Using strict data types, part of PHP: Strict Data Types.
PHP is increasingly being used…for sophisticated applications,…which benefit from strict data typing,…but the decision as to whether to use strict data types…is left up to the developer.…Their use is entirely optional.…Dynamic, loose typing with tight juggling…remains the default.…In fact, there's still no way to lock a variables data type.…Strict typing applies only to the arguments…past the functions and class methods.…And to the data type of the return value.…
Each declaration can be for only one data type.…You can't, for example, declare that a function…will return either a string or an array.…There's no key word for mixed data types.…Because this has been such a cause of controversy…in the developer community…strict data types have been implemented only gradually.…PHP 5.0 began by supporting parameter type declarations,…or type hints as they're called in PHP 5…for classes and interfaces.…
PHP 5.1 added support for parameter type hints for arrays,…and PHP 5.4 introduced the callable type…to specify for the function is required as an argument.…
First, he introduces the basics of type juggling and type casting in PHP. Then, he reviews strict typing with objects, arrays, and callback functions, which were introduced in PHP 5. He also reviews scalar type hints in PHP 7, which can have unexpected side effects for unwary developers. David concludes with a frank assessment of the practicalities of strict typing, and a simple recommendation that will help most developers use the new feature: define in strict and execute in weak.
- Implicit and explicit type casting
- Using strict data types
- Declaring return types in PHP 7
- Creating scalar parameter declarations and scalar return type declarations
- Assessing the merits of strict data types