HomePHP Page 2 - Using the Memento Pattern with a File Reading Class
Building an originator 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.
As I stated in the beginning, implementing the memento pattern requires at least two independent classes. Therefore, the first step that I'm going to take will consist of defining one of the primary classes that comprises the structure of this pattern.
I'm referring to the originator element, which in this case is aimed at building a comprehensive file reading class. Meanwhile, the caretaker class that I plan to create will be capable of holding the value of one particular property that belongs to this file reader, in this manner implementing a basic file navigation mechanism.
Now that I've outlined the general structure of the originator class, you'll want to take a look at its corresponding signature:
// define 'FileDataReader' class (in this case, this is the Originator class) class FileDataReader{ private $filePointer; private $fileData; public function __construct( $dataFile='default_data_file.txt',$filePointer=0){ if(!file_exists($dataFile)){ throw new Exception('Invalid data file!'); } $this->setFilePointer($filePointer); $this->setFileData($dataFile); } // set value for file pointer public function setFilePointer($filePointer){ if(!is_int($filePointer)||$filePointer<0){ throw new Exception('Invalid pointer for data file!'); } $this->filePointer=$filePointer; } // fetch value of file pointer public function getFilePointer(){ return $this->filePointer; } // read data from file public function setFileData($dataFile){ if(!$this->fileData=file($dataFile)){ throw new Exception('Error reading from data file!'); } } // return file data as array public function getFileData(){ return $this->fileData; } // fetch one line from data file public function fetchFileLine(){ if(!$fileLine=$this->fileData[$this->filePointer]){ throw new Exception('Error fetching line from file data!'); } return $fileLine; } }
As you can see, the above "FileDataReader" class is capable of performing some useful tasks, with reference to reading data from a specified text file. As to the functionality of this class, you'll realize that it's capable of fetching file data as an array structure, as well as retrieving one line of that file at once. Not too bad, right?
However, while I have to admit that the previous file reading class isn't going to change your life as a PHP developer, it does show in a nutshell how to define the structure of an originator. Naturally, any class can potentially fall under that category, but in this case the "FileDataReader" class is more than enough to demonstrate the functionality of the memento pattern.
At this stage, I'm pretty certain that you grasped the logic that drives the originator class. Therefore, in the following section I'll show you how to create the corresponding caretaker class. As I said when I explained how the caretaker class works, it will be capable of maintaining the value of a specific property.
To learn how this brand new class will be created, please click on the link shown below and keep reading.