PHP
  Home arrow PHP arrow Page 3 - The Basics of Using the Prototype Pattern with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

The Basics of Using the Prototype Pattern with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2007-05-15


    Table of Contents:
  • The Basics of Using the Prototype Pattern with PHP 5
  • Implementing the prototype pattern
  • Creating an additional prototype class
  • Testing the prototype pattern

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    The Basics of Using the Prototype Pattern with PHP 5 - Creating an additional prototype class
    ( Page 3 of 4 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT