https://github.com/peter279k/php-factory
The demonstration of PHP factory design pattern.
https://github.com/peter279k/php-factory
design-pattern factory-pattern php7 php71 php72
Last synced: 9 months ago
JSON representation
The demonstration of PHP factory design pattern.
- Host: GitHub
- URL: https://github.com/peter279k/php-factory
- Owner: peter279k
- Created: 2018-06-07T05:53:39.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-07T07:30:55.000Z (almost 8 years ago)
- Last Synced: 2025-02-10T07:13:51.125Z (about 1 year ago)
- Topics: design-pattern, factory-pattern, php7, php71, php72
- Language: PHP
- Size: 13.7 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# php-factory
[](https://travis-ci.org/peter279k/php-factory)
[](https://coveralls.io/github/peter279k/php-factory?branch=master)
## Introduction
It's about the factory design pattern implementation in this [blog post](https://www.startutorial.com/articles/view/understanding-design-patterns-abstract-factory#top).
## 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
```