If youíve ever developed desktop applications using mature object-oriented languages like C++ and Java, then itís possible that youíve found yourself saying a few funny phrases such as ďI need to implement a factory method within this class,Ē or in the worst case: ďthis class needs to implement a factory method, but I donít have a single clue about how to do that.Ē Surprisingly, you can implement these factory methods pretty quickly and easily in PHP 5, as this six-part series will show you.
To be honest, I grappled with factory methods in Java myself, but in the end I learned not only how to quickly build factory methods in Java classes, but how to implement those methods in PHP as well.
While it's undeniable that PHP is easier to learn than Java, many PHP developers with long experience in developing procedural programs may find it hard to grasp the foundations of the object-oriented paradigm, to say nothing of the implementation of design patterns that involve the use of objects, such as Singletons or the aforementioned Factory.
Fortunately, building factory methods in PHP is a simple process, which became even simpler with the release of PHP 5. But what is a factory method? In a nutshell, a factory method is a method that fabricates things and returns them when possible to client code.
In most cases, these things are objects, but they don't have to be. However, by far the most important facet of factory methods is their ability to create objects, which means that they play a relevant role when building object-oriented web programs, and therefore deserve a closer look.
So, given the importance of factory methods to developing web applications, in this group of tutorials Iím going to show how to implement this kind of method in PHP 5. I'll start with some basic code samples, and then, with the key concepts already digested, Iíll go into more complex examples that will combine the use of Singletons and static methods.
Now, itís time to start learning how to define a few simple factory methods with PHP 5. Itís going to be an instructive journey, trust me!