-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreational-prototype-2.php
More file actions
100 lines (83 loc) · 1.61 KB
/
creational-prototype-2.php
File metadata and controls
100 lines (83 loc) · 1.61 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Prototype interface - This abstract contain clone magic method
* 2nees.com
*/
abstract class Shape {
private int $x;
private int $y;
/**
* @param int $x
*/
public function setX(int $x): void
{
$this->x = $x;
}
/**
* @param int $y
*/
public function setY(int $y): void
{
$this->y = $y;
}
abstract public function __clone();
}
/**
* Simple Class for simulate Concrete Prototype
* 2nees.com
*/
class Rectangle extends Shape {
public function __clone()
{
return new Rectangle();
}
}
/**
* Simple Class for simulate Concrete Prototype
* 2nees.com
*/
class Circle extends Shape {
public int $radius;
public function __clone()
{
return new Circle();
}
}
/**
* Simple Class for simulate Concrete Prototype
* 2nees.com
*/
class Square extends Shape {
private int $position;
public function __construct() {
$this->position = 1;
$this->setX(1);
$this->setY(1);
}
public function __clone()
{
$size = rand(10, 20);
$this->position = rand(2, 40);
$this->setX($size);
$this->setY($size);
return $this;
}
}
$shapes = [];
$newCircle = new Circle();
$newCircle->radius = 20;
$newCircle->setX(5);
$newCircle->setY(5);
$newRect = new Rectangle();
$newRect->setX(10);
$newRect->setY(10);
$newSq = new Square();
$shapes[] = $newCircle;
$shapes[] = $newRect;
$shapes[] = $newSq;
for ($i = 1; $i <= 3; $i++){
$shapes[] = clone $newCircle;
$shapes[] = clone $newRect;
$shapes[] = clone $newSq;
}
print_r($shapes);