PHP
  Home arrow PHP arrow Page 2 - Handling Cookies and File Data with the Factory Pattern 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  
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

Handling Cookies and File Data with the Factory Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2007-07-02


    Table of Contents:
  • Handling Cookies and File Data with the Factory Pattern in PHP 5
  • Saving data using cookies and text files
  • Creating some data saving classes
  • Seeing the factory pattern 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


    Handling Cookies and File Data with the Factory Pattern in PHP 5 - Saving data using cookies and text files
    ( Page 2 of 4 )

    In accordance with the concepts expressed in the beginning of this article, in this case I'm going to illustrate the implementation of the factory pattern by creating a couple of concrete factory classes, which will be responsible for spawning different types of data saving objects.

    As you'll see shortly, the factory classes will be provided with the ability to return two specific kinds of objects to client code. The first kind will work in a predefined "string" context and the second kind will be used in an "object" environment.

    The previous explanation probably seems a bit disembodied if I don't show you the corresponding definitions for these classes, so here they are: 

    // define abstract 'DataSaverFactory' class
    abstract class DataSaverFactory{
       abstract public function createDataSaver($type);
    }

    // define first concrete factory
    class StringSaverFactory extends DataSaverFactory{
       private $context='string';
      
    public function createDataSaver($type){
        
    $dataSaver=NULL;
        
    switch($type){
          
    case "file";
            
    $dataSaver=new FileStringSaver();
            
    break;
          
    case "cookie";
            
    $dataSaver=new CookieStringSaver();
            
    break;
          
    default:
            
    $dataSaver=new FileStringSaver();
             
    break; 
         
    }
        
    return $dataSaver;
       
    }
    }

    // define second concrete factory
    class ObjectSaverFactory extends DataSaverFactory{
      
    private $context='object';
      
    public function createDataSaver($type){
        
    $dataSaver=NULL;
        
    switch($type){
          
    case "file";
            
    $dataSaver=new FileObjectSaver();
            
    break;
          
    case "cookie";
            
    $dataSaver=new CookieObjectSaver();
            
    break;
          
    default:
            
    $dataSaver=new CookieObjectSaver();
            
    break; 
         
    }
        
    return $dataSaver;
       
    }
    }

    After examining the respective signatures of the above factory classes, things are a bit clearer to you, right? As you can see, the first concrete class has been conceived to work in a predefined "string" context. It is tasked with creating two concrete types of objects, which save data strings to different locations, including text files and cookies.

    In a similar way, the second concrete factory class returns to client code objects whose primary task is to save other objects (not strings) to a specified file or cookie. Of course, due to the nature of the data with which this factory works, the class in question is designed to work in an "object" environment. Quite understandable, right?

    All right, I think that at this point you already understand how the pair of factory classes defined previously do their business. So it's a good time to move forward and start defining the data saving classes that work in the predefined contexts I explained above. 

    These brand new classes will be built in the course of the following section, therefore jump ahead and keep reading to learn more about them.



     
     
    >>> 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek