PHP
  Home arrow PHP arrow The Singleton and Factory Patterns in PHP: Working With Singletons
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

The Singleton and Factory Patterns in PHP: Working With Singletons
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2005-11-30


    Table of Contents:
  • The Singleton and Factory Patterns in PHP: Working With Singletons
  • Working with a single object instance: making the “formElementFactory” class a Singleton
  • The previous step: defining form element classes in PHP4
  • Ending up the coding round: defining the remaining form element classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    The Singleton and Factory Patterns in PHP: Working With Singletons
    ( Page 1 of 4 )

    In this fourth part of the series covering the Singleton and Factory Design Patterns in PHP, we will discuss issues stemming from the fact that PHP 4 does not have an abstract class. Since we found it useful in the previous article to define the form element factory class as an abstract class, in this article we will discuss the process for making the form element factory class a Singleton, and how this serves our purposes.

    Introduction

    Welcome to the fourth part of the series “The Singleton and Factory Design Patterns in PHP.” Throughout the previous article, I applied the Factory pattern for implementing an object-oriented method, in order to simplify the creation of regular web forms.

    As you remember, in the previous part, I developed a simple form element factory class that handles all of the tasks related to object instantiation. It allows you to define a centralized mechanism within an application for rendering form elements. Certainly, implementing this method makes web form generation a flexible and easy-to-maintain process.

    Now that I’ve explained the inherent advantages of applying the Factory pattern to tackle the form developing process, probably you’ll have even more ideas for starting to use design patterns across different applications.

    So, returning to this part of the series, the form element factory class was defined as an abstract class. The immediate advantage of having an abstract class rests on using its methods without the concerns related to object instantiation. This means that the functionality of the class is available within the program, but no objects exist to worry about.

    However, when we’re working with PHP 4, things are a bit more complex. Unfortunately, PHP 4 doesn’t offer support for abstract classes, although there is a workaround. This presents some difficulties, particularly when a single instance of an object is needed. In the specific case of having a class that instantiates form objects, it’s highly desirable to work with a single instance of it, instead of dealing with multiple and resource-consuming instances.

    Over the next few lines, I’ll explain the process for making the form element factory class a Singleton. This will help you to easily avoid multiple object instantiation issues, specifically when working on a PHP 4 development platform. Now that we have the theory well under the way, let’s get started.



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT