PHP
  Home arrow PHP arrow Page 3 - 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 - Creating some data saving classes


    (Page 3 of 4 )

    In the previous section I showed you the pertinent signatures of two concrete factories, which were originally conceived to work with different contexts, called "string" and "object" respectively. Also, it's worthwhile to notice here that the classes in question were capable of creating objects whose main task was saving different types of data entities, that is string and other objects, to simple text files and cookies.

    So, taking into account that it's necessary to define the classes that originate the aforementioned objects to complete the programmatic schema dictated by the factory pattern, now I'd like you to take a look at the following classes. They work with basic string and generic objects.

    Here they are: 

    // define abstract 'DataSaver' class
    abstract class DataSaver{
      
    abstract public function saveData();
      
    abstract public function getData();
    }

    // define concrete 'FileStringSaver' class
    class FileStringSaver extends DataSaver{
      
    private $data='This string will saved to target file.';
      
    public function saveData(){
        
    if(!$fp=fopen('filedata.txt','w')){
          
    throw new Exception('Error opening target file.');
        
    }
        
    if(!fwrite($fp,$this->data)){
          
    throw new Exception('Error writing data to target file.');
        
    }
        
    fclose($fp);
       
    }
      
    public function getData(){
        
    return $this->data;
      
    }
    }

    // define concrete 'CookieStringSaver' class
    class CookieStringSaver extends DataSaver{
      
    private $data='This string will be saved to target cookie.';
      
    public function saveData(){
        
    setcookie('default_cookie',$this->data);
      
    }
      
    public function getData(){
        
    return $this->data;
      
    }
    }

    // define concrete 'FileObjectSaver' class
    class FileObjectSaver extends DataSaver{
      
    private $inputObj;
      
    public function saveData(){
        
    if(!$fp=fopen('filedata.txt','w')){
          
    throw new Exception('Error opening target file.');
        
    }
        
    if(!fwrite($fp,serialize($this->inputObj))){
          
    throw new Exception('Error writing object data to target
    file.');
        
    }
        
    fclose($fp);
      
    }
      
    public function getData(){
        
    return $this->inputObj;
      
    }
      
    public function setInputObj($inputObj){
        
    if(!is_object($inputObj)){
          
    throw new Exception('Input data must be an object.');
        
    }
        
    $this->inputObj=$inputObj;
      
    }
    }

    // define concrete 'CookieObjectSaver' class
    class CookieObjectSaver extends DataSaver{
      
    private $inputObj;
      
    public function saveData(){
        
    setcookie('default_cookie',serialize($this->inputObj));
      
    }
      
    public function getData(){
        
    return $this->inputObj;
      
    }
      
    public function setInputObj($inputObj){
        
    if(!is_object($inputObj)){
          
    throw new Exception('Input data must be an object.');
        
    }
        
    $this->inputObj=$inputObj;
      
    }
    }

    As you can see, all the definitions of the above classes are actually very easy to follow, since they were introduced in the beginning of this section. However, let me go one step further and explain in detail how they work, in case you don't grasp the logic that stands behind these classes.

    There are two classes that are tasked with saving simple strings to plain text files and cookies, which implies that they should work with a predefined "string" context. Two additional classes are defined subsequently, this time with the purpose of storing generic objects in the same physical locations mentioned previously, meaning that they must be used in an "object" environment.

    So far, so good. At this point I hopefully demonstrated how the programmatic model imposed by the factory pattern can be easily implemented with PHP 5. On one hand, there are two concrete factory classes that work with a pair of specified environments, while on the other hand there are four "product" classes that are capable of saving certain data structures to cookies and text files.

    In simple terms, the previous scenario describes theoretically the way that the factory pattern works, but it's necessary to complement this theory with a practical example so you can see how all the classes defined earlier can be put to work together.

    Therefore, in the section to come I'm going to set up an example to show you more clearly how to use the factory classes that were developed previously.

    To see how this working example will be developed, please click on the link below and keep reading.

    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