HomePHP Page 3 - The Basics of Using the Prototype Pattern with PHP 5
Creating an additional prototype class - PHP
The prototype class lets you use many instances of a specific class, without copying objects to different variables. This article, the first of a two-part series, demonstrates the basic functionality provided by this pattern.
In consonance with the concepts deployed in the previous section, I'd like to complete the demonstration of how the prototype pattern works by defining an additional prototype class. This class will be aimed at working specifically with simple files. Of course, I'm going to create this brand new class by deriving it from the base "DataPrototype" that you learned earlier, meaning that it will also have the magic "__clone()" method, which was included in the other classes as well.
Having explained the basic functionality of this additional prototype class, here is how it looks:
// define concrete 'FilePrototype' class class FilePrototype extends DataPrototype{ private $file='default_file.txt'; public function setData($data){ if(!is_string($data)){ throw new Exception('Input data must be a string.'); } $this->data=$data; $this->save(); } public function getData(){ return $this->data; } public function getSize(){ if(!$size=filesize($this->file)){ throw new Exception('Error retrieving size of destination file.'); } return $size; } private function save(){ if(!$fp=fopen($this->file,'w')){ throw new Exception('Error opening destination file.'); } if(!fwrite($fp,$this->data)){ throw new Exception('Error writing data to destination file.'); } fclose($fp); } public function __clone(){} }
If you take some time and examine the signature that corresponds to the above "FilePrototype" class, then you'll see that it implements in a concrete way many of the methods defined by the corresponding parent. In this case, the class performs some useful tasks related to saving and loading strings from a specified text file.
You should also notice that the previous class declares the magic "__clone()" method, in this way sticking to the structure of the "ArrayPrototype" class that was created in the previous section.
Well, at this stage I think you already grasped the logic that stands behind the prototype pattern, since there are two classes that define the same "__clone()" method. This method is used for creating multiple instances of a specific class, starting from a unique object considered inside the pattern's context as the prototype.
Logically, as it was discussed in the introduction, the creation of multiple objects is performed by utilizing a cloning procedure. This is a process that certainly deserves a more detailed demonstration. Therefore, in the following section I'm going to set up a short -- yet educational -- example, which hopefully will show how some objects can be spawned by cloning the aforementioned prototype.
As you may have guessed, this testing example will be developed in the section to come, so jump ahead and read the next few lines. I'll be there, waiting for you.