Factory Design Pattern (simple example implementation in PHP)
Tags: php, designpatterns, factory
Published: 2021-02-06 19:19:22

---
title: Factory Design Pattern (simple example implementation in PHP)
published: true
description: factory pattern explained with a simple example implementation in PHP.
tags: PHP,designpatterns,factory
//cover_image: https://direct_url_to_image.jpg
---

**NOTE - Please everything written down here is extracted from a book which I read years ago and actually jotted down in a notebook (to be honest I don't remember the name of this book), I realized that keeping this information inside a notebook is not the best of options, come on what if something happened to my notebook? Also, I wanted this to be available to everyone who might have been wanting to understand the Factory Design Pattern as well -:)**

If you have not read my previous post on [Strategy Design Pattern](https://dev.to/osaigbovoemmanuel1/strategy-design-pattern-simple-example-implementation-in-php-41ae) Pattern, please do well to check it out.


The Factory design pattern becomes useful in situations where the type of object that needs to be generated isn't known when the program is written but only once the program is running.

Another clue for when the factory pattern might be appropriate is when there is an abstract base class and different subclasses will need to be created on the fly.

This particular design structure is important with the factory pattern, as once you have created the object, regardless of its specific type, the use of the object will be consistent.

The factory design pattern works via a state method,commonly named create(), factory(), factoryMethod() or createInstance(). The method takes at least one argument which indicates the type of object to create, the method returns an object of that type.
```
 Factory

 + create ($type) : Product
    .
    .
    .

Concrete Factory          ========>  Object of that class 
                                     type eg Product
+ create ($type) : Product 
```

In the factory design pattern, the abstract factory base class is extended by concrete factory classes that will output objects of that class type.

Let's see how this will be implemented in the following example. which will create a different type of shape based on the input provided to the page through the URL.

To create a Factory

1) Add a new PHP Script in your text editor or IDE to be named ShapeFactory.php

2) Start defining the ShapeFactory class:

```php

abstract class ShapeFactory {
 // the factory class will be abstract, just as the shape itself is. you will never create an instance of the ShapeFactory class. 
}
```

3) Start defining the static method
```php
public static function create($type, array $sizes) {
// this static method will take two arguments: the type of shape to create and an array of sizes. For a rectangle, that array would contain two values, for a triangle, three, and for a circle, only one (the radius)
```

4) create a different object based on that type of value
```php
switch($type) {
case 'rectangle'
 return new Rectangle($sizes[0], $sizes[1]);
break;
case 'triangle'
 return new Triangle($sizes[0], $sizes[1], $sizes[3]);
break;
case 'circle'
 return new Circle($sizes[0]);
break
}//end of switch
```
The switch checks the value of $type against the expected value for now just these shapes are recognized.

**NOTE - A complete version of this class would throw an exception if improper values were provided**

To use the ShapeFactory class
```php
$obj = ShapeFactory::create($GET['shape'], $GET['dimentions']);

/**
to create the object, the static method of the ShapeFactory class is invoked, passing it the value that came from the URL.
**/

/**
In a more realized version of this script, one would add error handling here to confirm that the shape object was created before attempting to use it in the following steps
**/
$area = $obj->getArea();

```