Home arrow PHP arrow Page 2 - Handling Cookies and File Data with the Factory Pattern in PHP 5

Saving data using cookies and text files - PHP

Any PHP developer who has been using the object-oriented paradigm for a while for developing web applications knows that the factory design pattern can be really useful when it comes to creating multiple objects across a specific programming context. However, if you're interested in filling some knowledge gaps that you might have about this handy pattern, then this article is possibly what you're looking for.

TABLE OF CONTENTS:
  1. Handling Cookies and File Data with the Factory Pattern in PHP 5
  2. Saving data using cookies and text files
  3. Creating some data saving classes
  4. Seeing the factory pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
July 02, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: