. PHP Design Patterns . PHP Design Patterns Prototype

PHP Design Patterns Prototype

About the Prototype


In the Prototype Pattern we create one standard object for each class, and clone that object to create new instances.

In this example we have an abstract BookPrototype class, with two specific or concrete subclasses, PHPBookPrototype and SQLBookPrototype. To create a object using either PHPBookPrototype or SQLBookPrototype we call the clone method.

BookPrototype.php


//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
  
  abstract class BookPrototype {

    protected $title;
    protected $topic;

    abstract function __clone();

    function getTitle() {return $this->title;}
    function setTitle($titleIn) {$this->title = $titleIn;}

    function getTopic() {return $this->topic;}

  }


download source, use right-click and "Save Target As..." to save with a .php extension.

PHPBookPrototype.php


//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved

  include_once('BookPrototype.php');

  class PHPBookPrototype extends BookPrototype {

    function __construct() {
      $this->topic = 'PHP';
    }

    function __clone() {
    }

  }


download source, use right-click and "Save Target As..." to save with a .php extension.

SQLBookPrototype.php


//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved

  include_once('BookPrototype.php');

  class SQLBookPrototype extends BookPrototype {

    function __construct() {
      $this->topic = 'SQL';
    }

    function __clone() {
    }

  }


download source, use right-click and "Save Target As..." to save with a .php extension.

testPrototype.php


//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved

  include_once('PHPBookPrototype.php');
  include_once('SQLBookPrototype.php');

  define('BR', '<'.'BR'.'>');

  echo 'BEGIN TESTING PROTOTYPE PATTERN'.BR;
  echo BR;

  $phpProto = new PHPBookPrototype();
  $sqlProto = new SQLBookPrototype();

  $book1 = clone $sqlProto;
  $book1->setTitle('SQL For Cats');
  echo 'Book 1 topic: '.$book1->getTopic().BR;
  echo 'Book 1 title: '.$book1->getTitle().BR;

  echo BR;

  $book2 = clone $phpProto;
  $book2->setTitle('OReilly Learning PHP 5');
  echo 'Book 2 topic: '.$book2->getTopic().BR;
  echo 'Book 2 title: '.$book2->getTitle().BR;

  echo BR;

  $book3 = clone $sqlProto;
  $book3->setTitle('OReilly Learning SQL');
  echo 'Book 3 topic: '.$book3->getTopic().BR;
  echo 'Book 3 title: '.$book3->getTitle().BR;

  echo BR.BR;
  echo 'END TESTING PROTOTYPE PATTERN'.BR;


download source, use right-click and "Save Target As..." to save with a .php extension.

output of testPrototype.php

BEGIN TESTING PROTOTYPE PATTERN

Book 1 topic: SQL Book 1 title: SQL For Cats

Book 2 topic: PHP Book 2 title: OReilly Learning PHP 5

Book 3 topic: SQL Book 3 title: OReilly Learning SQL

END TESTING PROTOTYPE PATTERN

References

Design Patterns

Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

PHP 5

The Official PHP web site
Core PHP Programming, 3rd Edition by Leon Atkinson and Zeev Suraski
Sign In
to add your own comment
Comment by Larry Rate this Comment

Good question on having the abstract __clone() function defined but not really used. It probably would make a more interesting example if one of the concrete clone methods did something.

Comment by sumwatt Rate this Comment

Out of curiosity, what is the purpose of declaring the abstract function __clone() in the parent object and then defining it in the children? Is it only to allow you some finite control over what happens when a specific object gets cloned? What would be the difference in just saying $copy = clone $original?