Drupal 8 Modules
Feature
This article will be more about the patterns you need to use during Drupal 8 development than how to fit the various pieces together.
There’s good reason for this approach: fitting the pieces together has plenty of examples, change records, and whatnot – but many pieces of the puzzle are entirely new to Drupal developers.
The Background
The first half of this article provides general PHP information which uses Drupal as an example, but is not Drupal specific. The idea behind this is that the knowledge can be reused well (indeed, this was also a design goal for Drupal 8).
Classes, Objects, Interfaces
In Drupal 7, stdClass
was used at a lot of places; ergo, classes (like stdClass
) and objects (instances of a class, like node, user, etc.) should be familiar. stdClass
is a class without methods, and the properties are not defined ahead of time. In PHP, it’s valid to set any property on an object, even if it’s not defined on its class. So, stdClass
worked much like an associated array, except that it used arrows instead of brackets. Another important distinction between arrays and objects is passing them to a function: in PHP5, if a function/method gets an object and then changes the object, it will affect the object everywhere – objects are not copied every time, while arrays are.