Home arrow PHP arrow 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.

  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



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){
       throw new Exception('Input data must be an array.');
   public function getData(){
     return $this->data;                     
   public function getSize(){
       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.

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