Home arrow PHP arrow Implementing Factory Methods in PHP 5

Implementing Factory Methods in PHP 5

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.

  1. Implementing Factory Methods in PHP 5
  2. The simplest object constructor
  3. Defining factory methods
  4. Factoring web form element objects
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
November 24, 2009

print this article



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!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: