PHP
  Home arrow PHP arrow Page 2 - The Singleton and Factory Patterns in ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: Building object-oriented forms
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 27
    2005-11-02

    Table of Contents:
  • The Singleton and Factory Patterns in PHP: Building object-oriented forms
  • When one is better than many: a quick look at the Singleton Pattern
  • Object-oriented forms: applying the Factory pattern to a real application
  • The first approximation to object-based forms: building form element classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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: Building object-oriented forms - When one is better than many: a quick look at the Singleton Pattern


    (Page 2 of 4 )

    The first step involved in the development of object-based forms consists of learning a little about the Singleton Pattern, because I'll use it within the form generation process.

    Probably, this pattern is one of the easiest ones to understand. To clearly explain its definition, let's describe a scenario which developers are frequently confronted with: one object is needed across an application by other objects for doing their work.

    Normally, in this situation, it's best to have a single instance of the required object for accessing within the application, instead of having multiple instances of it. To cite a common case, a single instance of a database connection object is required by different objects for them to have database connectivity in their scope.

    That's where the Singleton pattern comes in. Why trouble the application with multiple instances of an object, when a single instance is preferred?

    Usually, in PHP a class is turned into a Singleton by defining a static method "getInstance()" or even another indicative name, which when called, returns a single instance of the object. Subsequent calls to the method will always return a handle to the object instance.

    To clarify things, let's see an example based on the documentation provided in the PHP manual:

    class Singleton{
    // $instance holds a single instance of the object private static $instance; // private constructor private function __construct(){} // the getInstance() method returns a single instance of the object public static function getInstance(){ if(!isset(self::$instance)){ $object= __CLASS__; self::$instance=new $object; } return self::$instance; } public function displayMessage(){ echo 'I am a Singleton'; } } $singleton=Singleton::getInstance(); //
    return a single instance of the object $singleton->displayMessage();// display message

    The above class represents a simplified version of a Singleton. Notice the use of the static method "getInstance()", which when invoked the first time, returns a single instance of the object. As I said before, recurrent calls to the method will always return a handle to the object. 

     As you'll probably agree, this pattern is fairly understandable. Since this is only a brief overview, further and more complex implementation will be left as an additional task. However, for the purposes of the series, the example is indeed very illustrative.

    Now that we've demonstrated the functionality of the Singleton pattern, let's move one step toward the creation of object-based forms and explain the specific role of the Factory Pattern within the developing process.

    More PHP Articles
    More By Alejandro Gervasio


       · This first tutorial explains some of the key points related to the Singleton and...
       · Would perhaps be more useful if you actually introduced patterned code in this first...
       · Hello,Yes, there are more articles to come, which introduce the usage of the...
       · class Singleton{ var $instance; function Singleton(){} function...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway