PHP
  Home arrow PHP arrow Page 3 - The Basics of Using the Prototype Patt...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 9
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · This first tutorial of the series shows how to implement the prototype design...
       · What is the difference between cloning a class/object and creating new instances of...
       · Thank you for posting your comments on my PHP article. Concerning your question, the...
       · Sorry the question was not the difference between copying and cloning, but rather...
       · When you create an instance of a class in PHP 5, by default this instance is a...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway