PHP
  Home arrow PHP arrow Page 2 - The Singleton and Factory Patterns in PHP: Building object-oriented forms
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: Building object-oriented forms
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 29
    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:
      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: 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
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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