-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.php
More file actions
77 lines (71 loc) · 2.1 KB
/
factory.php
File metadata and controls
77 lines (71 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
//Factory Design Pattern
$cars = [
"nissan" => [
"sunny" => [
'make' => 2004,
'model' => 'Nissan Sunny B14',
'displacement' => '1503cc',
'feature'=>'Some Special Features For Filder 2004'
],
"sunny-ex" => [
'make' => 2005,
'model' => 'Nissan Sunny Ex Saloon',
'displacement' => '1300cc',
'feature'=>'Some Special Features For Filder 2004'
],
],
"toyota" => [
"axio" => [
'make' => 2004,
'model' => 'Toyota Corolla Axio',
'displacement' => '1500cc',
'feature'=>'Some Special Features For Axio 2004'
],
"filder" => [
'make' => 2005,
'model' => 'Toyota Corolla Fielder',
'displacement' => '1500cc',
'feature'=>'Some Special Features For Filder 2004'
],
],
];
class Car {
private $make, $model, $displacement, $feature;
function __construct($carData){
$this->make = $carData['make'];
$this->model = $carData['model'];
$this->displacement = $carData['displacement'];
$this->feature = $carData['feature'];
}
function __call($method, $arguments=null){
$parameter = str_replace('get', '', strtolower($method));
if(isset($this->{$parameter})){
return $this->{$parameter}."\n";
}
return '';
}
}
class CarFactory {
private $data;
function __construct($data){
$this->data = $data;
}
function getNissanSunny(){
$data = $this->data['nissan']['sunny'];
return new Car($data);
}
function getToyotaAxio(){
$data = $this->data['toyota']['axio'];
return new Car($data);
}
}
// $nissanSunny = new Car($cars['nissan']['sunny']);
// echo $nissanSunny->getModel();
$carFactory = new CarFactory($cars);
$nissanSunny = $carFactory->getNissanSunny();
$toyotaAxio = $carFactory->getToyotaAxio();
echo $nissanSunny->getDisplacement();
echo $nissanSunny->getModel();
echo $toyotaAxio->getMake();
echo $toyotaAxio->getFeature();