- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
Latest commit
107 lines (88 loc) · 2.63 KB
/
Copy pathindex.php
File metadata and controls
107 lines (88 loc) · 2.63 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
101
102
103
104
105
106
107
<?php
// ** Classes, objects, methods and properties
// ** The $this keyword
// ** indicates that we use the class's own methods and properties, and allows us to have access to them within the class's scope.
// Create and declare Class
class Car {
// add properties
public$comp;
public$color = 'beige';
public$hasSunRoof = true;
public$tank;
private$model = 'N/A';
publicfunction__construct($model = null)
{
// Only if the model value is passed it will be assigned
if($model) {
$this -> model = $model;
}
}
// add methods
publicfunctionhello()
{
return"Beep I am a <i>" . $this -> comp . "</i>" . ", and I am " . "<i>" . $this -> color . "</i>";
}
// Add gallons of fuel to the tank when we fill it.
publicfunctionfill($float)
{
$this -> tank += $float;
return$this;
}
// Substract gallons of fuel from the tank as we ride the car.
publicfunctionride($float)
{
$miles = $float;
$gallons = $miles / 50;
$this -> tank -= $gallons;
return$this;
}
publicfunctiongetModel()
{
return__CLASS__ . " The car model is {$this -> model}";
}
}
// ** Declare an inheritance aka child class
//The child class can use the code it inherited from the parent class,
class SportsCar extends Car {
private$style = 'fast and furious';
publicfunctiondriveItWithStyle()
{
return'Drive a ' . $this -> getModel() . ' <i>' . $this -> style . '</i>';
// return 'Drive a ' . $this -> getModel() . ' <i>' . $this -> style . '</i>';
}
// override parent Class's method
publicfunctionhello()
{
return"Brrrrr... <i>" . $this -> comp . "</i>" . ", and I am " . "<i>" . $this -> style . "</i>";
}
}
$lambo = newSportsCar('LB-007');
echo$lambo -> getModel();
echo'<br>';
echo$lambo -> hello();
echo'<br>';
// Creat instance objects from a class
$bmw = newCar();
$mercedes = newCar();
// Set the values
$bmw -> comp = "BMW";
$bmw -> color = "blue";
$mercedes -> comp = "Mercedes Benz";
$mercedes -> color = "grey";
echo$bmw -> color;
echo'<br>';
echo$mercedes -> comp;
echo'<br>';
// Use the mothods to get a beep
echo$bmw -> hello();
echo'<br>';
echo$mercedes -> hello();
// Use the chaining methodes and properties
$volvo = newCar();
$tank = $volvo -> fill(10) -> ride(40) -> tank;
echo"The number of gallons left in the tank: " . $tank . " gal.";
echo'<br>';
// Access private property using Setter and Getter
$audi = newCar('Hello-192');
echo$audi -> getModel();
echo'<br>';