PHP
  Home arrow PHP arrow Page 4 - 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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
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 - 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT