Typed Drupal - A great combination of Drupal 8 and PHP 7
Update : Link to the Slide Deck : https://drive.google.com/open?id=0B7hrQ7OurTKjTlU1Z3FGMGN5SFE
There are many advantages of a strongly typed language. Strong typing is a communication tool for explicit statements of intent backed by the guard of compilation. Strong typing has the following features:
- Errors / Warnings in your editor as you code.
- Better overall code readability.
- Ease of Refactoring
- Less bugs in your code.
Traditionally, PHP is a Weakly typed / Dynamically typed language. So, as a result, when we wrote custom modules in Drupal, we were not able to use strong typing and all other benefits typing provides. Also, PHP tries to convert the given variable to the type of the data value it is assigned when the types don't match (called Type Juggling), this can be a pain point and cause unexpected issues in your program. But, now with the advent of PHP 7, we can write typed code and there is an optional strict mode too!, How cool is that?
In this session, we will look at how we can use typed PHP code and write custom modules in Drupal 8. Writing Custom modules in Drupal 8 with PHP 7 is a very satisfying experience as we'll see in this session. We'll also see how to code modules in Drupal using strict typing mode of PHP 7.
We will cover the following topics in this session:
- Strongly Typed vs Weakly Typed Debate, Who wins?
- Typing in PHP 7
- Scalar Type Hints in PHP 7 and How to use them.
- Return Type declarations and how to use them.
- Using Strict Typing mode in PHP 7
- Handling Type error exceptions.
- Designing "Typed" Drupal modules with PHP 7.
Finally, we'll see how creating 'Typed' Drupal modules in PHP 7 is a winning combination and will help us build better, stable and bug-free modules.