HomePHP Page 2 - The Basics of Using the Prototype Pattern with PHP 5
Implementing 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.
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.