HomePHP Page 2 - Handling File Data with the Facade Pattern in PHP 5
Using the facade pattern to handle file data - PHP
Are you interested in learning the foundations of different structural patterns in PHP 5? If your answer is affirmative, then you should start reading this article immediately! Welcome to the last installment of the series “Introducing the Facade Pattern in PHP 5.” In two consecutive articles, this series shows you how to create and use facade classes in PHP-based development environments.
As I said in the introduction, before I proceed to create a facade class I’m going to define a simple yet efficient file processing class, which, as you’ll see in a moment, will be capable of reading and writing data to a given text file.
However, once the file handling class has been created, I’m going to show you how it can be used in tandem with the facade class in question. So, for the moment, I suggest you pay strong attention to the definition for the class, which looks like this:
// define 'FileProcessor' class class FileProcessor{ private $dataFile; private $fileData; public function __construct($dataFile='datafile.txt', $data='This string of data will be saved to file!'){ if(!file_exists($dataFile)){ throw new Exception('Invalid path for data file!'); } $this->dataFile=$dataFile; if(!data){ throw new Exception('Invalid data to be saved on data file!'); } $this->data=$data; } // read data from file public function readData(){ if(!$content=file_get_contents($this->dataFile)){ throw new Exception('Error reading data from data file!'); } return $content; } // write data to file public function writeData(){ if(!$fp=fopen($this->dataFile,'w')){ throw new Exception('Error opening data file!'); } if(!fwrite($fp,$this->data)){ throw new Exception('Error writing data to file!'); } fclose($fp); } }
As shown above, the logic implemented by the “FileProcessor” class is indeed easily followed. Basically, all this class does is save an input string to a specified text file, via the respective “writeData()” method, and write the string to the same file, in this particular case by the means of “readData().” So far there's nothing unexpected, right?
Based upon the functionality offered by this simple file processing class, below I listed a short script, which exemplifies the use of this class. Have a look at the following code sample, please:
// example using 'FileProcessor' class try{ // instantiate new 'FileProcessor' object $fileProc=new FileProcessor(); // write data to text file $fileProc->writeData(); // read and display data from text file echo $fileProc->readData(); } catch(Exception $e){ echo $e->getMessage(); exit(); }
In this case, the above script shows clearly how the “FileProcessor” class that I created a few lines above can be used in a useful way. As you can see, the previous code snippet first instantiates a new file processor object, then writes a default input string to the specified data file, and finally displays this data on the browser.
Of course, this example would be rather incomplete if I didn't show you the corresponding output produced by previous script, which is as follows:
'This string of data will be saved to file!
All right, at this point I’m quite sure that you understand how the “FileProcessor” class does its thing. I assume you're also intrigued by how this class can be an important piece of the facade pattern. Therefore, in the next section I’m going to create a facade class which will be capable of applying different formats to an inputted string, including all the data read by the file processing class that was covered before.
To see how this entirely new facade class will be defined, go ahead and read the following section. I’ll be there, waiting for you.