PHP
  Home arrow PHP arrow Page 4 - 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 - Seeing the factory pattern in action
    ( Page 4 of 4 )

    If you're anything like me, you want to see how the group of sample classes defined in the previous section can be used together in a practical example. In this case, I'm going to code a short script, which hopefully will illustrate how each of the factory classes that you leaned before can be statically called to create different data saving objects.

    Since the two pertinent "product" classes that work with a specified "object" context accept an object as their unique input parameter, I will define a sample "User" class which will be utilized by these classes.

    Here is the corresponding signature for the "User" class:

    // define 'User' class
    class User{
       
    private $name;
      
    private $email;
      
    public function __construct($name='John
    Doe',$email='john@domain.com'){
        
    $this->name=$name;
        
    $this->email=$email;
      
    }
      
    public function getName(){
        
    return $this->name;
      
    }
      
    public function getEmail(){
        
    return $this->email;
      
    }
    }

    If you study the definition of the previous "User" class, surely you'll realize that it is very easy to grasp, so I'm not going to waste your time with irrelevant explanations about how it functions. Instead, I'd like you to pay attention to the following script, which demonstrates how all the prior factory classes can be used to spawn different types of data saving objects.

    The corresponding code sample is as follows:

    try{
      
    // create file string saver object
      
    $fileStringSaver=StringSaverFactory::createDataSaver('file');
      
    // save sample string to target file
      
    $fileStringSaver->saveData();
      
    // display sample string
      
    echo $fileStringSaver->getData();

       /*
      
    displays the following:
      
    This string will saved to target file.
      
    */

       // create cookie string saver object
      
    $cookieStringSaver=StringSaverFactory::createDataSaver
    ('cookie');
      
    // save sample string to target cookie
      
    $cookieStringSaver->saveData();
      
    // display sample string
      
    echo $cookieStringSaver->getData();

       /*
      
    displays the following:
      
    This string will be saved to target cookie.
      
    */

       // create file object saver object
      
    $fileObjectSaver=ObjectSaverFactory::createDataSaver('file');
      
    // create new user object
      
    $user=new User();
      
    $fileObjectSaver->setInputObj($user);
      
    // save user object to target file
      
    $fileObjectSaver->saveData();
      
    // return user object to client code
      
    $newUser=$fileObjectSaver->getData();
      
    // display user properties
      
    echo 'Values for name and email properties are the following :
    '.$newUser->getName().' '.$newUser->getEmail();

       /*
      
    displays the following:
      
    Values for name and email properties are the following : John
    Doe john@domain.com
       */

       // create cookie object saver object
      
    $cookieObjectSaver=ObjectSaverFactory::createDataSaver
    ('cookie');
      
    $cookieObjectSaver->setInputObj($user);
      
    // save user object to target cookie
      
    $cookieObjectSaver->saveData();
    }

    catch(Exception $e){
      
    echo $e->getMessage();
      
    exit();
    }

    As shown in the above script, all the factory classes are called statically (note the use of the :: scope resolution operator) with the purpose of creating distinct data saving objects. For each specific case, the script also displays the corresponding outputs, something that helps in understanding more clearly how the factor pattern works.

    I encourage you to use all the factory and product classes defined in the course of this article and introduce your own modifications to them. This will give you a more robust background in this particular pattern.

    To wrap up

    In this second article of the series, I provided you with another hands-on example surrounding the implementation of the factory design pattern with PHP 5. Nevertheless, this educational journey hasn't ended yet, since I'm going to finish this series by showing you how to use this handy pattern in a real world situation: building a set of context boxes, which can be easily included into any web page.

    The experience sounds really interesting, so I hope to see you in the last part!



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