-
Notifications
You must be signed in to change notification settings - Fork 0
/
flights_example.php
114 lines (93 loc) · 1.81 KB
/
flights_example.php
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
*=============================================================
* <flights_example.php>
* Problem: Using unset() and shuffle() give notices as
* 'Indirect modification of overloaded property has no effect'
*
* @Author Muhammad Anwar Hussain<[email protected]>
* Created on: 10th July 2019
* ============================================================
*/
trait FlightInfo
{
public function callFlight($flightName)
{
return new $flightName;
}
}
trait PassengerInfo
{
public function passengerInfo($terminalName)
{
echo "[$terminalName]: Passenger information for ".strtoupper(__CLASS__ ). "<br>";
}
}
class Terminal_1
{
use FlightInfo;
}
class Terminal_2
{
use FlightInfo;
}
class Terminal_3
{
use FlightInfo;
}
class Flight_A
{
use PassengerInfo;
}
class Flight_B
{
use PassengerInfo;
}
class Flight_C
{
use PassengerInfo;
}
class Flight_D
{
use PassengerInfo;
}
class Flight_E
{
use PassengerInfo;
}
class Container
{
protected $data = array();
function __set($k, $v)
{
$this->data[$k] = $v;
}
function __get($k)
{
if (!isset($this->data[$k]))
{
throw new Exception(sprintf('Key "%s" does not exists.', $k));
}
return is_callable($this->data[$k])? $this->data[$k]($this) : $this->data[$k];
}
}
$c = new Container();
$c->flights = ['Flight_A', 'Flight_B', 'Flight_C', 'Flight_D', 'Flight_E'];
$c->terminals = ['Terminal_1', 'Terminal_2', 'Terminal_3'];
if(isset($c->flights[1]))
{
unset($c->flights[1]);
}
if(isset($c->flights[4]))
{
unset($c->flights[4]);
}
shuffle($c->flights);
shuffle($c->terminals);
for ($indx = 0; $indx < count($c->terminals); $indx++)
{
$c->terminal = function($c) use($indx){return new $c->terminals[$indx];};
$c->flight = $c->terminal->callFlight($c->flights[$indx]);
$c->flight->passengerInfo($c->terminals[$indx]);
}
?>