Skip to content

The demonstration of PHP factory design pattern.

Notifications You must be signed in to change notification settings

peter279k/php-factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-factory

Build Status Coverage Status

Introduction

It's about the factory design pattern implementation in this blog post.

Usage

Here is the example of creating the NyCar and NyHelicopter class instance via NyToysFactory.

NyCar class instance:

$nyToyFactory = new NyToysFactory();
$nyCar = $nyToyFactory->createToy('car'));

echo $nyCar->price; //30
echo $nyCar->name; //NyCar

NyHelicopter class instance:

$nyToyFactory = new NyToysFactory();
$nyHelicopter = $nyToyFactory->createToy('helicopter'));

echo $nyHelicopter->price; //300000
echo $nyHelicopter->name; //NyHelicopter

Prepare the NyCar

$nyToyFactory = new NyToysFactory();
$nyCar = $nyToyFactory->createToy('car');
$nyCar->prepare();
$wheels = $nyCar->wheels; //array count is 4
foreach($wheels as $nyWheel) {
    echo $nyWheel->name; //wheel
    echo $nyWheel->number; //1
}

Prepare the NyHelicopter

$nyToyFactory = new NyToysFactory();
$nyHelicopter = $nyToyFactory->createToy('helicopter');
$nyHelicopter->prepare();

$nyHelicopter->engine->name; //engine
$nyHelicopter->rotorBlade->number; //1

About

The demonstration of PHP factory design pattern.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages