-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraits.php
114 lines (95 loc) · 2 KB
/
traits.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
/**
*=============================================================
* <traits.php>
* object oriented programming: Traits in PHP
*
* @author Muhammad Anwar Hussain<anwar_hussain.01@yahoo.com>
* Created on: 10th June 2019
* ============================================================
*/
trait FlightServices
{
abstract public function seat();
abstract public function food();
public function serviceXYZ()
{
echo __METHOD__.'<br>';
}
}
trait FlightInfo
{
public function airline($name)
{
echo 'Welcome to '. $name.'<br>';
}
public function ticketFare($cost)
{
echo 'Ticket Price: '. $cost .'<br>';
}
public function luggageWeight($weight)
{
echo 'Allowed Luggage Weight: '. $weight .'<br>';
}
}
class Economic
{
use FlightInfo;
use FlightServices;
public function seat()
{
echo 'Seat: standard'.'<br>';
}
public function food()
{
echo 'Food: good'.'<br>';
}
}
class Business
{
use FlightInfo;
use FlightServices;
public function seat()
{
echo 'Seat: Luxurious'.'<br>';
}
public function food()
{
echo 'Food: Excellent'.'<br>';
}
}
class FlightBooking
{
private $airline;
private $cost = [];
private $weight = [];
public function setFlight($name, $weight = [], $cost = [])
{
$this->airline = $name;
$this->weight = $weight;
$this->cost = $cost;
}
protected function ticket($c)
{
$class = strtolower(get_class($c));
$c->airline($this->airline) .'<br>';
echo 'Class: '. strtoupper($class) .'<br>';
$c->serviceXYZ();
$c->seat();
$c->food();
$c->luggageWeight($this->weight[$class]);
echo '------------------------------------<br>';
$c->ticketFare($this->cost[$class]);
}
public function confirmFlight($category)
{
$category = new $category;
$this->ticket($category);
}
}
$f = new FlightBooking();
$f->setFlight('Biman Bangladesh Airlines', ['economic' => '30 kg', 'business'=> '50 kg'], ['economic' => '$ 950', 'business'=> '$ 1500']);
$f->confirmFlight('Economic');
echo '<br>';
$f->confirmFlight('Business');
?>