in oop php object oriented programming design pattern programming ~ read.

Prototype Pattern In PHP

In this post, we are going to talk about how to use Prototype Pattern in PHP.

What is Prototype Pattern and Why?

First let's talk about what is Prototype Pattern. Prototype Pattern belongs to Creational Category of Design Pattern which means it deals with the creation of objects. and there are a lot of reasons why people choose to use Prototype Pattern, have a look at the following:

  1. Avoid costly creation
  2. Avoid subclassing
  3. Typically doesn't use new
  4. Often utilizes an interface

For me the most decisive factor for me to use Prototype pattern is to avoid using too many new to create an object.

How to implement Prototype Pattern in PHP

Let's say we have an Item abstract class from which we are going to extend some sub class.

<?php

abstract class Item {  
  private $title;
  private $price;
  private $url;

  public function getTitle()
  {
    return $this->title;
  }

  public function setTitle($title)
  {
    $this->title = $title;
  }

  public function getPrice()
  {
    return $this->price;
  }

  public function setPrice($price)
  {
    $this->price = $price;
  }

  public function getUrl()
  {
    return $this->url;
  }

  public function setUrl($url)
  {
    $this->url = $url;
  }
}

(Item)

Two subclasses that extend from Item.

<?php  
use Item

class Book extends Item {  
  private $numberOfPages;

  public function getNumberOfPages()
  {
    return $this->numberOfPages;
  }

  public function setNumberOfPages($numberOfPages)
  {
    $this->numberOfPages = $numberOfPages;
  }
}

(Book)

<?php  
use Item

class Movie extends Item {  
  private $runtime;

  public function getRuntime()
  {
    return $this->runtime;
  }

  public function setRuntime($runtime)
  {
    $this->runtime = $runtime;
  }
}

(Movie)

Yip they are looking pretty normal before implementing Prototype Pattern. Normally we will just use new to create a new objects of Movie or Book class. But image if you have like 2 billions of objects you need to create, it will be so tiresome to use new. So let's implement Prototype Pattern next.

The reason why Prototype Pattern could create objects for us without using new keyword is that, it uses clone to do it with two options, shadow clone or deep clone. And that's why it can be a way more faster to create objects cause computers are so good at copy/paste.

Let's modify our Item to handle the clone functionality.

<?php

abstract class Item {  
  private $title;
  private $price;
  private $url;

  abstract function __clone();


  public function getTitle()
  {
    return $this->title;
  }

  public function setTitle($title)
  {
    $this->title = $title;
  }

  public function getPrice()
  {
    return $this->price;
  }

  public function setPrice($price)
  {
    $this->price = $price;
  }

  public function getUrl()
  {
    return $this->url;
  }

  public function setUrl($url)
  {
    $this->url = $url;
  }
}

(Item)

Next let's create Register class to handle the creation of objects.

<?php

use Book;  
use Movie;

class Registry {  
  private $items;

  public function __construct()
  {
    $this->loadItems();
  }

  public function createItem($type)
  {
    $item = null;

    try {
      $item = $this->items->clone();
    } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }

    return $item;
  }

  public function loadItems()
  {
    $movie = new Movie();
    $movie->setTitle("Basic Movie");
    $movie->setPrice(24.99);
    $movie->setRuntime("2 Hours");
    $this->items[] = $movie;

    $book = new Book();
    $book->setNumberOfPages(335);
    $book->setPrice(24.99);
    $movie->setTitle("Basic Book");
    $this->items[] = $book;
  }
}

(Registry)

Next let's create some objects using Prototype Pattern.

<?php

use Registry;

$registry = new Registry();
$movie = $registry->createItem("Movie");
$movie->setTitle("Creational Pattern in PHP");

echo $movie;  
echo $movie->getRuntime();  
echo $movie->getTitle();  
echo $movie->getUrl();

$anotherMovie = $registry->createItem("Movie");
$anotherMovie->setTitle("Gang of Four");

echo $anotherMovie;  
echo $anotherMovie->getRuntime();  
echo $anotherMovie->getTitle();  
echo $anotherMovie->getUrl();  

End

The reason to use Prototype Pattern is rather obvious that if you find yourself constantly creating a lot of objects. Hope you guys can learn something from here and if you have any ideas of comments please leave it below, Thanks!

comments powered by Disqus