Typed Drupal - A great combination of Drupal 8 and PHP 7

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:

  1. Errors / Warnings in your editor as you code.
  2. Better overall code readability.
  3. Ease of Refactoring
  4. 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:

  1. Strongly Typed vs Weakly Typed Debate, Who wins?
  2. Typing in PHP 7
  3. Scalar Type Hints in PHP 7 and How to use them.
  4. Return Type declarations and how to use them.
  5. Using Strict Typing mode in PHP 7
  6. Handling Type error exceptions.
  7. 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.