Home arrow PHP arrow Page 4 - The Basics of Using the Prototype Pattern with PHP 5

Testing the prototype pattern - PHP

The prototype class lets you use many instances of a specific class, without copying objects to different variables. This article, the first of a two-part series, demonstrates the basic functionality provided by this pattern.

  1. The Basics of Using the Prototype Pattern with PHP 5
  2. Implementing the prototype pattern
  3. Creating an additional prototype class
  4. Testing the prototype pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
May 15, 2007

print this article



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:

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

   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-

   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();

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: