Join David Powers for an in-depth discussion in this video Assessing the merits of strict data types, part of PHP: Strict Data Types.
- [Voiceover] Now that we've explored…strict data types in PHP,…it's time to assess how they've been implemented.…To be honest, the handling of strict data types…brings to mind this famous 19th century cartoon…about the curate's egg.…When asked if he's got a bad egg,…the curate replies, "Oh no, parts of it are excellent."…So what works well?…Parameter declarations for objects, arrays,…and callable functions that have been available…since PHP 5 work really well.…
Return type declarations for the same data types…that were introduced in PHP 7 work equally well.…They both have the advantages…not only of being unambiguous,…but they're also entirely optional.…If you use them, the specified data types…are enforced, but there's no type juggling…going on behind the scenes.…For that reason, scalar type declarations…in strict mode also work well…as long as you remember to enable strict mode…consistently throughout an application.…
And that's the main criticism…of scalar type declarations.…If you want to enforce strict data types,…
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