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


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 3 hosted by Hostway
    Stay green...Green IT