Factory Pattern in PHP

Introduction

Building a website or application can be grainy, because you will need to create objects, but it is very difficult to know what type. Suppose a factory manufactures various kinds of products, each product can be made of any number of components in their inventory. The workers know what's in the inventory but don't necessarily know beforehand what kind of products they will be making. Now if we talk about a programming language, in an application, you have many classes; you have a set of component classes but won't know exactly which one you will need to instantiate until runtime. The answer of this question is the Factory Pattern. The short answer is if you have many classes and you need to choose one at runtime. For this process, you can use the Factory Pattern. Now I will show you, how to implement a Factory Pattern in your application.

Step 1


Create an abstract class for future use. 

The following sample declares two abstract methods with the names Iphone() and Simplephone() and both methods will be implemented in the other non-abstract class. See:

abstract class AbstractMobilePhoneFactory
{

abstract function Iphone();

abstract function Simplephone();

}

Step 2

Create a class that extends the abstract class for implementation.

The following class PhoneName extends the abstract class for implementation. I have taken two private variables and implemented all abstract methods in this class. See:

class PhoneName extends AbstractMobilePhoneFactory
{

private $createIphone;

private $createSimplephonee;

private function _construct(){

$this->$createIphone="Apple";

$this->$createSimplephone="NOKIA";

}

public function Iphone(){

return $this->$createIphone;

}

function Simplephone(){

return $this->$createSimplephone;

}

}

Step 3

Create a class that extends the abstract class for implementation. Basically, I declare a class as abstract, because you can extend an abstract class with more than one normal class for a different implemntation.

In the following code, the class PhonePrice is used for the price detail of various kinds of phoned using an abstract class. See:

class
PhonePrice extends AbstractMobilePhoneFactory
{

private
$PriceIphone;
private
$PriceSimplephone;
private
function _construct(){
$
this->$PriceIphone="$750";
$
this->$PriceSimplephone="$$200";
}

public
function Iphone(){
return
$this->$PriceIphone;
}
function Simplephone(){

return
$this->$$PriceSimplephone;
}

}

Step 4

Now, create a Factory Pattern for the above classes.


The following class with the name FactoryPattern defines a function getPhoneDetail($dtl), with parameter "dtl". It has a switch case that determines which class object will be manufactured. See:

class
FactoryPattern {
public
function getPhoneDetail($dtl){
$MobilePhone=NULL;

switch
($dtl)
{

case
"Name":
  $MobilePhone=
new PhoneName;
 
break;
case
"Price":
    $MobilePhone=
new PhonePrice;
    
break;
default
:
   $MobilePhone=
new PhoneName;
 
break;     
}
}

}


Step 5

In the following code, the FactoryPattern class code has been created. Using this object, you can call the getPhoneDetail($dtl) method, to decide which class object is created at run time.
Create object of FactoryPattern class


$factory=
new FactoryPattern();
$name =$factory->getPhoneDetail(
"Name");
$price =$factory->getPhoneDetail(
"Price");

Example of Factory Pattern

<?php?
abstract class AbstractMobilePhoneFactory
{
abstract
function Iphone();
abstract
function Simplephone();

//Phone name class

class PhoneName extends AbstractMobilePhoneFactory{

private $createIphone;

private $createSimplephonee;

private function _construct(){

echo 'I am constructed. '."</br>";

$this->$createIphone="Apple";

$this->$createSimplephone="NOKIA";

}

public function Iphone(){

return $this->$createIphone;

}

function Simplephone(){

return $this->$createSimplephone;

}

}

 

 

//Phone price class

class PhonePrice extends AbstractMobilePhoneFactory{

private $PriceIphone;

private $PriceSimplephone;

private function _construct(){

$this->$PriceIphone="$750";

$this->$PriceSimplephone="$$200";

}

public function Iphone(){

return $this->$PriceIphone;

}

function Simplephone(){

return $this->$$PriceSimplephone;

}

}

 

 

//FactoryPatternClass

class FactoryPattern {

public function getPhoneDetail($dtl){

$MobilePhone=NULL;

switch($dtl)

{

case "Name":

  $MobilePhone=new PhoneName;

  break;

 

case "Price":

    $MobilePhone=new PhonePrice;

     break;

default:

   $MobilePhone=new PhoneName;

  break;     

}

}

}

 

//Text factory pattern

$factory= new FactoryPattern();

$name =$factory->getPhoneDetail("Name");

$price =$factory->getPhoneDetail("Price");

?>

    }

}
?>

The Factory Pattern is used in the following situations:

  • A class can not anticipate the type of object it needs to create beforehand.
  • You want to localize the logic to instantiate a complex object.
  • The advantage of a Factory Method is that it can return the same instance multiple times, or can return a subclass rather than an object of that exact type.

Up Next
    Ebook Download
    View all
    Learn
    View all