-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayObject.php
141 lines (116 loc) · 2.61 KB
/
ArrayObject.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
*=============================================================
* <ArrayObject.php>
* Object Oriented Programming: ArrayObject built-in class in PHP
* All the previous problems are solved through the example
* @Author Muhammad Anwar Hussain<anwar_hussain.01@yahoo.com>
* 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 extends \ArrayObject
{
protected $data=array();
public function offsetExists ($offset)
{
return isset($this->data[$offset]);
}
function offsetSet($offset, $value)
{
if (is_null($offset))
{
$this->data[] = $value;
}
else
{
$this->data[$offset] = $value;
}
}
function &offsetGet($offset)
{
if (!isset($this->data[$offset]))
{
throw new Exception(sprintf('Key "%s" is not defined.', $offset));
}
$this->data[$offset] = is_callable($this->data[$offset]) ? $this->data[$offset]($this) : $this->data[$offset];
return $this->data[$offset];
}
public function offsetUnset($offset)
{
unset($this->data[$offset]);
}
}
$c = new Container();
$c['flight.names'] = ['Flight_A', 'Flight_B', 'Flight_C', 'Flight_D', 'Flight_E'];
$c['terminal.names'] = ['Terminal_1', 'Terminal_2', 'Terminal_3'];
if(isset($c['flight.names'][1]))
{
unset($c['flight.names'][1]);
}
if(isset($c['flight.names'][2]))
{
unset($c['flight.names'][2]);
}
shuffle($c['terminal.names']);
shuffle($c['flight.names']);
$length = count($c['terminal.names']);
for ($c['indx'] = 0; $c['indx'] < $length; $c['indx']++)
{
$c['terminal'] = function($c){return new $c['terminal.names'][$c['indx']];};
$c->flight = $c['terminal']->callFlight($c['flight.names'][$c['indx']]);
$c->flight->passengerInfo($c['terminal.names'][$c['indx']]);
}
echo '<br>';
for ($c['indx'] = $length -1; $c['indx'] >= 0; $c['indx']--)
{
$c['terminal'] = function($c){return new $c['terminal.names'][$c['indx']];};
$c->flight = $c['terminal']->callFlight($c['flight.names'][$c['indx']]);
$c->flight->passengerInfo($c['terminal.names'][$c['indx']]);
}
?>