Drupal 8 Development Mastery (4): Modern PHP Programming: Factory methods

In this article I continue my exploration of the fundamentals of Drupal 8 development. I explain what the "Factory Method" programming pattern is, why it is important and it's relevance to Drupal 8 development mastery.

Modern PHP Programming:  Factory methods

This is the fourth part in the series in which I aim to explore mastering the basics of Drupal 8 development. 

In the first part, I outlined the road map for this Drupal 8 mastery journey. In the second, I reviewed the road map. In the third part, I started on the first definitive topic: Modern PHP Programming: Programming Patterns. There I introduced the idea of programming patterns or design patterns.

And in this fourth part I will introduce a specific example of a design pattern that is relevant to Drupal 8, the Factory methods. This topic is also the second item within the Technology Stack section of our road map under PHP Programming Pattern.

In the last part I explained what a design/programming pattern is in very simple terms and gave references to the best sources I could find that gives further details. In case you are still fuzzy about what they are, please revisit that part before proceeding.

Be warned, to understand design patterns you must have some familiarity with object-oriented programming. From now on I will l be talking a lot about ‘classes’ and 'objects'. So if you are not comfortable with these OOP ideas, you may want to work through some of the OOP references I gave in the previous part. If you grab enough of OOP, well, lets go.

Here I will try to explain the PHP factory method programming pattern and how it is used in Drupal 8 as much as I understand it. I am no authority in this, but I intend to learn by teaching. So bear with me.

So, What Exactly Is The Factory Method?

The factory method is one of the most commonly used design pattern generally, not just in PHP. The overall benefit of the factory, as well as that of all design patterns, is that it makes working on complex programming projects a whole lot easier.

So in simple terms, a factory method is a subroutine or an object (OOP class context) that returns another object. As the name implies, the goal of the factory object is to ‘create’ other objects that you can then utilize in other parts of your application or code. While this method of writing code is not absolutely necessary, it is very powerful.

In simple terms, a factory method is a subroutine or an object that returns another object.

So, Why Is The Factory Method So Important?

The main benefit of this is that you can now modify the class of the produced object at any time without needing to make changes to any other part of your code besides the factory class (the principle of loose coupling).

Secondly, the factory hides the actual implementation details of the objects it produces from codes that uses it. As a result you don't have to know how the object produced is implemented to use it. You just use it and it works, like magic.

For instance, a piece of code elsewhere can establish a connection to a database using a factory method without knowledge of the type of database in use (ie whether it is MySQL, Oracle, DBase, Ms SQL server). The code just passes the database credentials to the factory. The factory then handles the actual tasks of determining the exact database in use, making the connection and returning the results to the calling code.

Some Code Example, Please!

All of these may sound abstract if you have not actually made use of them in a real software development situation. As with every software programming ideas the best way to fully understand it is to work through actual code. So here is a simple PHP factory method implementation taken from here (http://www.phptherightway.com/pages/Design-Patterns.html).


class Automobile
    private $vehicleMake;
    private $vehicleModel;

    public function __construct($make, $model)
        $this->vehicleMake = $make;
        $this->vehicleModel = $model;

    public function getMakeAndModel()
        return $this->vehicleMake . ' ' . $this->vehicleModel;

class AutomobileFactory
    public static function create($make, $model)
        return new Automobile($make, $model);

// have the factory create the Automobile object
$veyron = AutomobileFactory::create('Bugatti', 'Veyron');

print_r($veyron->getMakeAndModel()); // outputs "Bugatti Veyron"


Here, the AutomobileFactory class is the factory, while the Automobile class is the product. The factory is used to produce products of a specific type. In this case our factory class, AutomobileFactory, is used to produce an Automobile object of 'Make' Bugatti and 'Model' Veyron. This is a simplified example but it captures the basic ideas of the factory method very beautifully.

But don't be fooled by this simplicity. In a more complex situation it can be very powerful and highly desirable.

So, How Is the Factory Method Relevant To Drupal 8?

Well, I am no expert in Drupal 8, yet. But these I know:

With Drupal 8, Drupal was rebuilt completely using the object-oriented style. Unlike previous Drupal versions. At present no software project of such magnitude can be built and maintained successfully without leveraging the powers of popular design patterns like the Factory Methods. So be assured the factory method design pattern is used extensively in Drupal 8.

Besides, numerous Drupal 8 articles across the web mentions the factory method design pattern as one of the principle methods used in many of Drupal 8 new constructs. These includes key constructs like Plugins, Services, etc. I will be touching on these topics later in this series.

Next: Modern PHP Programming: Dependency Injection


Add new comment