PHP
  Home arrow PHP arrow Page 2 - Handling Cookies and File Data with th...
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Handling Cookies and File Data with the Factory Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      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


    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


       · In this second part of the series, you’ll learn how to apply the factory pattern in...
       · Thanks!
       · Thank you for the kind comments on my PHP article. It's good to know it's been...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT