PHP
  Home arrow PHP arrow Page 4 - 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 - Testing the prototype pattern
    ( Page 4 of 4 )

    Certainly, I could spend my time and yours explaining theoretically how the prototype pattern works, but I know that you're expecting to see a concrete example that illustrates how multiple instances of a prototype object can be created during the execution of a specific script.

    Therefore, I coded a brief example (shown below), which shows in a friendly fashion how this task is performed via the cloning process that I mentioned previously.

    The source code that corresponds to the example in question looks like this:

    try{
       // create new 'ArrayPrototype' object
       $arrayPrototype=new ArrayPrototype();
       // clone 'ArrayPrototype' to create another object 
       $array1=clone $arrayPrototype;
       $array1->setData(array('This is element 1','This is element
    2','This is element 3'));
       echo 'Number of array elements is as following :'.$array1-
    >getSize();

       /*
       displays the following
       Number of array elements is as following :3
       */

       $array2= clone $arrayPrototype;
       $array2->setData(array('This is element A','This is element
    B','This is element C','This is element D'));
       echo 'Number of array elements is as following :'.$array2-
    >getSize();

       /*
       displays the following
       Number of array elements is as following :4
       */

       // create new 'FilePrototype' object
       $filePrototype=new FilePrototype();
       // clone 'FilePrototype' to create another object
       $file1=clone $filePrototype;
       $file1->setData('This string will be saved to file!');
       echo 'Size in bytes of destination file is as
    following :'.$file1->getSize();

       /*
       displays the following
       Size in bytes of destination file is as following :34
       */
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As you can see, the above code snippet shows in a nutshell how the prototype pattern works, since it uses the built-in PHP "clone()" method to create different instances of a prototype object. More specifically speaking, the first case demonstrates how to spawn two different array objects by cloning their respective prototype, while the second one uses the same approach, but this time with a pair of file-related objects.

    Do you grasp the logic followed by the prototype pattern? I bet you do!

    Finally, my last suggestion concerning the implementation of this unusual pattern doesn't differ too much from using other ones: try testing and tweaking all the classes shown here, to give you a more robust background in how this pattern works. I'm sure you'll have a good time!

    Final thoughts

    In this first article of the series I walked you though the basics of implementing the prototype pattern with PHP 5. Hopefully, all the hands-on examples coded here will help you expand your existing skills in pattern-based programming.

    In the final tutorial of the series, I'm going to demonstrate how to use this handy pattern to develop and expandable data validation application.

    You've been warned, so don't miss it!



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