Module Development

Object Composition FTW! for Drupal 8, 9

Sprout Video

In modern PHP, you're going to spend a lot of time working with other people's classes: via external libraries that you bring into your project to get things done faster. Of course, when you do that: you can't actually edit their code if you need to change or add some behavior.

Fortunately, object-oriented code gives us some really neat ways to deal with this limitation. In this tutorial, you'll learn a method called composition in which we'll create a wrapper class, which has some subtle advantages over using inheritance.

Additional resources

Object-Oriented PHP (Topic) (Drupalize.Me)