HomePHP Page 2 - Manipulating String Literals with Interpreter Classes in PHP 5
Handling literals - PHP
Among the huge variety of design patterns that can be easily implemented in PHP 5, the interpreter is one that might be particularly interesting to you. It allows you to build effortlessly the so-called parser layers. Welcome to the second installment of the series that began with “Building Interpreter Classes with PHP 5.” Made up of three parts, this series introduces the core concepts on this useful pattern, and also complements its theory with numerous educational hands-on examples.
In accordance with the concepts expressed in the introduction, I’m going to demonstrate how to implement the interpreter pattern by developing a simple mechanism which will come in handy for processing basic string literals.
Essentially, I’m going to define a class which will perform a few useful tasks. These tasks will include loading and saving an inputted string to a specified text file, as well as calculating the length of the string in question. As you can see, all these tasks sound fairly easy to execute, meaning that the structure of this string processing class is very simple.
And speaking of that, here is the definition for this class, so take a look at its source code:
// define 'StringSaver' class class StringSaver{ private $dataFile; private $data; public function __construct($data='This is the default string',$dataFile='datafile.txt'){ if(empty($data)){ throw new Exception('Input data must not be an empty string!'); } if(!file_exists($dataFile)){ throw new Exception('Invalid destination file!'); } $this->data=$data; $this->dataFile=$dataFile; } // load string from destination file public function loadString(){ if(!$this->data=file_get_contents($this->dataFile)){ throw new Exception('Error reading string from destination file!'); } return $this->data; } // save string to destination file public function saveString(){ if(!$fp=fopen($this->dataFile,'w')){ throw new Exception('Error opening destination file!'); } if(!fwrite($fp,$this->data)){ throw new Exception('Error writing string to destination file!'); } fclose($fp); } // determine length of string public function getStringLength(){ if(!$length=strlen($this->data)){ throw new Exception('Error determining length of data string!'); } return $length; } }
Not surprisingly, the signature that corresponds to the previous “StringSaver” class is really easy to grasp. As I said before, the tasks performed by this class are limited to loading and saving a given string to a target text file, along with returning the length of that string to calling code.
However, at this point there’s not a single clue about how to include an interpreter class that can work in conjunction with the string processor that you learned a few lines above. In the next section, I’m going to define the basic structure of an interpreter class, in this way implementing the programmatic model dictated by the homonymous pattern.
Having said that, and assuming that you’re interested in learning how this interpreter class will be built, I suggest you click on the link that appears below and read the following section.