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