- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract.php
More file actions
Latest commit
81 lines (66 loc) · 1.76 KB
/
Copy pathabstract.php
File metadata and controls
81 lines (66 loc) · 1.76 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
<?php
echo'<h1>Abstract</h1>';
// Abstract classes are declared with the abstract keyword, and contain abstract methods.
// ** cannot creat objects from abstract classes so needs to create child classes
abstractclass Car {
// Abstract classes can have properties
protected$tankVolume;
// Abstract classes can have non abstract methods
publicfunctionsetTankVolume($volume)
{
$this -> tankVolume = $volume;
}
// Abstract method
abstractpublicfunctioncalcNumMilesOnFullTank();
}
// Create child classes from an abstract class
class Honda extends Car {
// ** The child classes that inherit from abstract classes must add bodies to the abstract methods.
publicfunctioncalcNumMilesOnFullTank()
{
$miles = $this -> tankVolume*30;
return$miles;
}
}
class Toyota extends Car {
publicfunctioncalcNumMilesOnFullTank()
{
return$miles = $this -> tankVolume*33;
}
// its own method
publicfunctiongetColor()
{
return"beige";
}
}
$toyota1 = newToyota();
$toyota1 -> setTankVolume(10);
echo$toyota1 -> calcNumMilesOnFullTank();
echo'<br>';
echo$toyota1 -> getColor();
echo'<br>';
echo'<hr>';
echo'<br>';
// Practice
abstractclass User {
protected$username;
publicfunctionsetUsername($username) {
$this -> username = $username;
}
publicfunctiongetUsername() {
return$this -> username;
}
abstractpublicfunctionstateYourRole();
}
class Admin extends User {
publicfunctionstateYourRole(){
return"admin";
}
}
class Viewer extends User {
publicfunctionstateYourRole(){
returnstrtolower(__CLASS__);
}
}
$Balthazar = newAdmin();
echo$Balthazar -> stateYourRole();