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

Seeing the factory pattern in action - 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

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
 

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: