PHP
  Home arrow PHP arrow Page 3 - 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 - 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
     

       

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