PHP
  Home arrow PHP arrow Page 2 - 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? 
Google.com  
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 - Implementing the prototype pattern
    ( Page 2 of 4 )

       

    To demonstrate how the prototype pattern works (at least basically for the moment), I'm going to define a bunch of sample classes for establishing the basic structure of different types of data. Then, using these classes, I'll show you how to create multiple instances of a specific class with the prototype pattern. 

    Okay, now that I have explained my plan, please study the signatures of the classes below, which outline the generic structure of a highly-abstracted data entity.

    The definitions corresponding to these brand new classes are as follows: 

    // define 'DataPrototype' class
    abstract class DataPrototype{
       private $size;
       private $data;
       abstract public function __clone();
       abstract public function getData();
       abstract public function setData($data);
       abstract public function getSize();
    }

    // define concrete 'ArrayPrototype' class
    class ArrayPrototype extends DataPrototype{
       public function setData($data){
         if(!is_array($data)){
           throw new Exception('Input data must be an array.');
         }
         $this->data=$data;
       }
       public function getData(){
         return $this->data;                     
       }
       public function getSize(){
         if(!$size=count($this->data)){
           throw new Exception('Error retrieving size of array');
         }
         return $size;
       }
       public function __clone(){}
    }

    As shown above, the first sample class has been defined as abstract and its signature is merely an interface for defining the generic behavior of a prototype data entity. Concerning the structure of this base class, you can see that it presents some basic modifiers and accessing methods, which shouldn't be hard to grasp at all.

    On the other hand, the second class, called "ArrayPrototype," is a subclass of the previous "DataPrototype." It naturally implements concretely most of its methods to work specifically with array structures.

    So far, so good. At this point I have defined two basic prototype classes, which doesn't present any major difficulties for the average PHP developer. However, undeniably in this case there's a characteristic associated with the previous classes that deserves special attention. Please notice how both of them declare the magic "__clone()" method, available with PHP5.

    What's the point of declaring this method? Well, according to the intrinsic definition of the prototype pattern, multiple instances of the same class can be created by cloning a prototype object. Therefore the existence of the method makes sense here, since it will be responsible for cloning objects that might be needed by a concrete application. Sounds pretty logical, right?

    All in all, I built a prototype class that defines some straightforward methods for working with arrays. However, I believe that you'll understand much more easily how the prototype pattern works if I derive another subclass from the base "DataPrototype." In this case, it will be useful for working with files.

    Want to learn how this brand new prototype class will be built? Okay, go to the following section and keep reading.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek