- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.php
More file actions
Latest commit
104 lines (84 loc) Β· 2.47 KB
/
Copy pathinterface.php
File metadata and controls
104 lines (84 loc) Β· 2.47 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
<link rel="stylesheet" href="css/styles.css">
<?php
echo'<h1>Interface</h1>';
// An interface commits its child classes to abstract methods that they should implement.
// Declare & implement an interface
interface Car {
publicfunctionsetModel($name);
publicfunctiongetModel();
}
interface Vehicle {
publicfunctionsetHasWheels($bool);
publicfunctiongetHasWheels();
}
class miniCar implements Car, Vehicle {
private$model;
private$hasWheels;
publicfunctionsetModel($model)
{
$this -> model = $model;
}
publicfunctiongetModel()
{
return$this -> model;
}
publicfunctionsetHasWheels($bool)
{
$this -> hasWheels = $bool;
}
publicfunctiongetHasWheels()
{
return$this -> hasWheels ? "has wheels" : "no wheels";
}
}
$Pony = newminiCar();
$Pony -> setModel("P-10");
echo$Pony -> getModel();
echo'<br/>';
$Pony -> setHasWheels(false);
echo$Pony -> getHasWheels();
echo'<br>';
echo'<hr>';
echo'<br>';
// Practice
class User {
protected$username;
publicfunctionsetUsername($username) {
$this -> username = $username;
}
publicfunctiongetUsername() {
return$this -> username;
}
}
interface Author {
publicfunctionsetAuthorPrivileges($array);
publicfunctiongetAuthorPrivileges();
}
interface Editor {
publicfunctionsetEditorPrivileges($array);
publicfunctiongetEditorPrivileges();
}
// Create a class that extends User and implements interfaces
class AuthorEditor extends User implements Author, Editor {
private$authorPrivilegesAray = array();
private$editorPrivilegesAray = array();
publicfunctionsetAuthorPrivileges($array) {
$this -> authorPrivilegesAray = $array;
}
publicfunctiongetAuthorPrivileges() {
return$this -> editorPrivilegesAray;
}
publicfunctionsetEditorPrivileges($array) {
$this -> editorPrivilegesAray = $array;
}
publicfunctiongetEditorPrivileges() {
return$this -> editorPrivilegesAray;
}
}
// Create ab object
$user1 = newAuthorEditor();
$user1 -> setUsername("Bogus");
$user1 -> setAuthorPrivileges(array("write text", "add punctuation"));
$user1 -> setEditorPrivileges(array("edit text", "edit punctuation"));
$userPrivileges = array_merge($user1 -> getAuthorPrivileges(), $user1 -> getEditorPrivileges());
echo$user1 -> getUsername() . " has the following privileges: " . implode(", ", $userPrivileges);