PHP
  Home arrow PHP arrow Using Singletons with Factory Methods in PHP 5
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 
iPad news and developer info.
Ads by affinity 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
VPS Hosting 
Forums Sitemap 
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

Using Singletons with Factory Methods in PHP 5


By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2009-12-01


    Table of Contents:
  • Using Singletons with Factory Methods in PHP 5
  • Review: building a basic factory class with PHP 5
  • Coding a Singleton method
  • The enhanced factory class in action

  • 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


    Summary: In this second installment of a six-part series on implementing the factory pattern in PHP 5, I explain how to build an improved version of a factory class that returns Singletons of itself to client code.

    Using Singletons with Factory Methods in PHP 5
    (Page 1 of 4 )

    While this may sound like a cliché, the truth is that building factory methods in PHP 5 is a straightforward process that only requires an intermediate background in working with objects and classes. After all, a factory method is merely a regular class method that returns, when possible, one or more objects to client code -- or expressed in other terms, it’s a concrete implementation of the Factory design pattern.

    In reality, learning the basics of factory methods is one of those things that every PHP developer must tackle sooner or later. They're used very frequently in the development of object-oriented web applications, which makes it practically impossible not to deal directly with them at some point.

    Of course, if you already went through the introductory part of this series, then you have a pretty clear idea of how to create factory methods using PHP 5. In that tutorial I demonstrated how to create a simple factory class which could spawn a couple of web form element objects for rendering on screen a few basic HTML input boxes and text areas.

    However, this example factory class has a serious down side. In its current version it’s necessary to create an instance of it to dynamically call its factory method, and therefore spawn the mentioned web form element objects. Actually, this is considered a bad thing in terms of programming habits, because in this particular case the goal of the factory class is to build other objects, other than its own..so what’s the point in dealing with instances of it? That’s pretty useless.

    To address only partially the issue described above, in this second chapter of the series I’m going to introduce some subtle changes into the definition of this factory class, so it can return Singletons of it to client code. This at least will make sure that only one instance of it will be created during the execution of a given script.

    Now, let’s and see how to translate the previous concepts to fully-functional PHP 5 code. Let’s go!



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

       

    PHP ARTICLES

    - PHP Excel Customizations: Working with Formu...
    - MVC and the Zend Framework
    - PHP: Rendering Web Pages Using the Composite...
    - PHP: Creating Dynamic Web Pages with the Com...
    - Secure Encrypting and Decrypting for Your PH...
    - PHP: Creating Single View Objects with the C...
    - PHP: View Object Collections and the Composi...
    - Securing Your PHP Website
    - Determine Link Relevance and Unique Class C ...
    - PHP Composite View Design Pattern: Introduci...
    - Facebook PHP API Applications: A Second Look
    - Secure PHP Programming
    - Facebook PHP API Applications: Basic Introdu...
    - SOAP Servers and Web Services
    - Using XPath, SOAP, and More with Web Services


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code



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