Home arrow PHP arrow Page 3 - Using the Memento Pattern with a File Reading Class

Defining the signature of a caretaker class - PHP

The memento pattern can be used to maintain the state of a property that belongs to a specific class. If you want to learn more about how to do this, start reading this tutorial! Welcome to the final installment of the series "Maintaining the State of Classes." In two parts, this series teaches you how to use the memento design pattern with PHP 5, and it accompanies the corresponding theoretical concepts with diverse hands-on examples.

  1. Using the Memento Pattern with a File Reading Class
  2. Building an originator class
  3. Defining the signature of a caretaker class
  4. The memento design pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
January 15, 2007

print this article



As you might have guessed, the functionality of a caretaker class has to be strongly related to the nature of the tasks performed by the originator. Keeping in mind this basic principle, below I listed the definition for the corresponding caretaker. In a fit of inspiration, I named it "FileDataSeeker." Take a look at it, please:

// define 'FileDataSeeker' class (in this case, this is the
Caretaker class)
class FileDataSeeker{
   private $filePointer;
   // the constructor takes up the originator object as the only
input parameter
   public function __construct(FileDataReader $fileDataReader){
   public function setFilePointer(FileDataReader $fileDataReader){
   public function getFilePointer(FileDataReader $fileDataReader){

As illustrated above, the "FileDataSeeker" class has some useful methods for handling the "filePointer" property that belongs to the respective originator class. These methods will help this caretaker class to carry out its responsibility to store the value of the property. Thus, the value can be retrieved at a later time. Pretty logical, isn't it?

By providing the "FileDataSeeker" class with this ability, if the file reading class locates its file pointer at a non-existent place, it will be possible to move it back to a valid position via the respective caretaker. Definitely, that makes a lot of sense!

However, showing you the originator and the caretaker separately won't give you a clear idea of how they work. Thus, in response to this issue, in the next section I'll create a simple script, which hopefully will demonstrate the power of these two classes when they operate together.

As usual, to see how this will be achieved, go ahead and read the following section. I'll be there, waiting for you.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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