Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. The Basics of Using the Prototype Pattern with PHP 5
  2. Implementing the prototype pattern
  3. Creating an additional prototype class
  4. Testing the prototype pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
May 15, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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