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? 
Google.com  
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

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek