Handling Cookies and File Data with the Factory Pattern in PHP 5

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.

Introduction

Welcome to the second tutorial of the series that began with "Using the Factory Pattern in PHP 5." Made up of three instructive parts, this series walks you through the foundations of developing factory classes with PHP, and also shows you how to implement this popular design pattern in real world situations.

Now, having properly introduced the subject of this series, let me touch upon what was covered in the preceding article to refresh your memory. As you’ll probably recall, I demonstrated in a friendly way how to build a pair of concrete factory classes. These came in useful for spawning different array-processing objects in accordance with the specifications of their corresponding contexts.

Of course, defining the aforementioned factory classes was merely an introduction to exploring the remarkable functionality provided by the homonymous pattern, and it should be admitted that there are many other cases in which the pattern can be used successfully. Nevertheless, I do think that all the hands-on examples shown in the first article of the series were explicit enough to illustrate how a concrete factory class works and how it can be called statically within a specific programming environment.

So far, so good right? At this stage you should have a more accurate idea of how to include the factory pattern into your own PHP applications, at least at a very basic level. But don’t you worry because this scenario is going to change quickly, since in this second tutorial I’m going to teach you how to take advantage of the capacity offered by the pattern to create objects that are capable of saving data to different locations, including files and cookies.

Hopefully, this experience will be educational and serve to expand your existing skills on pattern-based programming with PHP 5 even more.

So, are you ready to start reading? Let’s go!

{mospagebreak title=Saving data using cookies and text files}

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.

{mospagebreak title=Creating some data saving classes}

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.

{mospagebreak title=Seeing the factory pattern in action}

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!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan