PHP
  Home arrow PHP arrow Page 3 - Polymorphism, Design Patterns, and PHP Programming
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

Polymorphism, Design Patterns, and PHP Programming
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2006-10-05


    Table of Contents:
  • Polymorphism, Design Patterns, and PHP Programming
  • Interfaces and Type Hints
  • The Factory Pattern
  • The Singleton Pattern

  • 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


    Polymorphism, Design Patterns, and PHP Programming - The Factory Pattern
    ( Page 3 of 4 )

    The Factory pattern provides a standard way for a class to create objects of other classes. The typical use for this is when you have a function that should return objects of different classes, depending on its input parameters.

    One of the major challenges in migrating services to a different database is finding all the places where the old wrapper object is used and supplying the new one. For example, say you have a reporting database that is backed against an Oracle database that you access exclusively through a class called DB_Oracle_Reporting:

    class DB_Oracle_Reporting extends DB_Oracle { /*
    ... */}

    and because you had foresight DB_Oracle uses our standard database API.

    class DB_Oracle implements DB_Connection { /* ...
    */ }

    Scattered throughout the application code, whenever access to the reporting database is required, you have wrapper instantiations like this:

    $dbh = new DB_Oracle_Reporting;

    If you want to cut the database over to use the new wrapper DB_Mysql_Reporting, you need to track down every place where you use the old wrapper and change it to this:

    $dbh = new DB_Mysql_Reporting;

    A more flexible approach is to create all your database objects with a single factory. Such a factory would look like this:

    function DB_Connection_Factory($key)
    {
     switch($key) {
      case "Test":
       return new DB_Mysql_Test;
      case "Prod":
       return new DB_Mysql_Prod;
      case "Weblog":
       return new DB_Pgsql_Weblog;
      case "Reporting":
       return new DB_Oracle_Reporting;
      default:
       return false;
     }
    }

    Instead of instantiating objects by using new, you can use the following to instantiate objects:

    $dbh = DB_Connection_factory("Reporting");

    Now to globally change the implementation of connections using the reporting interface, you only need to change the factory.



     
     
    >>> More PHP Articles          >>> More By Sams Publishing
     

       

    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 3 Hosted by Hostway
    Stay green...Green IT