HomePHP The Basics of Using the Factory Pattern in PHP 5
The Basics of Using the Factory Pattern in PHP 5
If you need to create multiple objects that belong to the same family, you probably want to use the factory pattern. This three-part series takes a close look at using the factory pattern in PHP.
Many PHP developers with a solid background in pattern-based programming know that there are certain patterns that are more difficult to grasp than others. This is due mostly to the inherent diversity of this fascinating terrain, where purist concepts about software design merge flawlessly with simple, natural common sense.
Indeed, design patterns present singularly interesting facets. They are conceived as proven solutions to specific programming-related issues, but at the same time, have a certain dose of improvisation, which makes them even more attractive. Logically, if you think carefully about this concept, you'll understand more clearly the meaning of this question: how many times did you find yourself applying a particular pattern without knowing what you were actually doing? Perhaps dozens, even hundreds of times.
Nevertheless, the most important thing to notice here is that a concrete problem can be solved by implementing the logic of a particular pattern, which means that certain software development issues can be addressed in a more intuitive way than others. And definitely, the factory pattern is an excellent example of this situation, since many developers use it frequently, even if they're taking their first steps into the object-oriented programming arena.
Admittedly, it's common to see during the development of a given PHP application that one or more classes are created with the purpose of returning a specific type of object to client code, where the object must conform to the specifications of a concrete environment. As you may have guessed, this is precisely the theoretical definition of the factory pattern.
Summarizing, the factory pattern can be really useful when it comes to creating multiple objects that belong to the same family. In this three-part series I'm going to take a close look at it, and also demonstrate its remarkable functionality by showing you a decent variety of code samples, so you can start quickly including this pattern into your own PHP applications.
With the preliminaries out of the way, now let's move on together and begin discovering how to apply the factory pattern with PHP. It's going to be an educational journey, believe me!