Home arrow PHP arrow Page 4 - Introducing the Memento Pattern

Implementing the memento pattern - PHP

PHP developers frequently need to maintain the state of an object across several web pages. There are a number of ways to do this. In this article, the first of two parts, you will learn how to handle this task with the memento pattern.

TABLE OF CONTENTS:
  1. Introducing the Memento Pattern
  2. Creating the first piece of the memento pattern
  3. Building the second piece of the memento pattern
  4. Implementing the memento pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
January 08, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said at the end of the section that you just read, the best way to understand the logic that drives the memento pattern is by creating a hands-on example that shows in a step-by-step format how a caretaker object can hold the state of a property that belongs to an originator.

Based upon this premise, below I set up the example in question. It  demonstrates progressively how the previously created "ArrayElementSelector" class has the capacity to retain the value of the "arrayIndex" property that belongs to the "ArrayProcessor." The corresponding code sample is as follows:

try{
    // example using Originator and Caretaker classes
    // instantiate new 'ArrayProcessor' object
    $arrayProcessor=new ArrayProcessor(array('This is element 1','This is element 2','This is element 3','This is element 4','This is element 5'));
    // instantiate new 'ArrayElementSelector' object
   $arrayElemSelector=new ArrayElementSelector($arrayProcessor);
   // display first element of input array
   echo 'First element of input array is the following: '.$arrayProcessor->getArrayElement();           

   /* displays the following:
   First element of input array is the following: This is element 1
    */           

    $arrayProcessor->setArrayIndex(1);
    $arrayElemSelector->setArrayIndex($arrayProcessor);
    // caretaker object holds up the value of array index
    $arrayElemSelector->setArrayIndex($arrayProcessor);
    echo 'Second element of input array is the following: '.$arrayProcessor->getArrayElement();

    /*
    displays the following:
    Second element of input array is the following: This is element 2
    */

    // move array index to nonexistent element of input array
    $arrayProcessor->setArrayIndex(10);
    echo $arrayProcessor->getArrayElement();

    /* 
    displays the following:
    Error fetching element of input array!
    */

    // move array index back to second element of input array by using caretaker object
    $arrayElemSelector->getArrayIndex($arrayProcessor);
    $arrayProcessor->getArrayIndex();
    echo 'Second element of input array is the following: '.$arrayProcessor->getArrayElement();

   /*
   displays the following:
   Second element of input array is the following: This is element 2
   */

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

As illustrated above, after instantiating the two objects required by the memento pattern, that is the originator (in this case, represented by the ArrayProcessor class), and the caretaker (as you know, the "ArrayElementSelector class), the first element of the inputted array is properly displayed.

However, the actual capacity of the pattern is demonstrated when the "ArrayElementSelector" object stores the value of the "arrayIndex" property, before moving this index forward and trying to display a non-existent array element:

// caretaker object holds up the value of array index
$arrayElemSelector->setArrayIndex($arrayProcessor);
// move array index to nonexistent element of input array
$arrayProcessor->setArrayIndex(10);
echo $arrayProcessor->getArrayElement();

Obviously, this condition fires up an exception and the script is halted. But, since the caretaker has previously maintained the state of the "arrayIndex" property, it's possible to go back and redisplay the second element of the inputted array. As you can imagine, this situation is demonstrated by the following lines of code:

// move array index back to second element of input array by using caretaker object
$arrayElemSelector->getArrayIndex($arrayProcessor);
$arrayProcessor->getArrayIndex();
echo 'Second element of input array is the following: '.$arrayProcessor->getArrayElement();

Now, after understanding how the originator and caretaker classes work together, I'm sure that you'll agree with me that the memento pattern comes in handy for maintaining the state of a particular class. Isn't this great?

Wrapping up

Over this first installment of the series, I explained the basics of how to apply the memento pattern in PHP 5. Aside from being introduced to the concepts of the originator and caretaker classes respectively, hopefully you learned how to store the state of a given class by using another one, which is indeed useful.

However, this journey is not finished. In the last article of the series, I'll show you how to use the memento pattern to hold the state of a file reading class. I hope to see you there!



 
 
>>> 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: