forked from assembler-institute/oop-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13-interfaces.php
73 lines (59 loc) · 2.15 KB
/
13-interfaces.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
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================
/* File 13 - Interfaces */
// Interfaces allows specifying what methods a class should implement.
// they make it easy to use a variety of different classes in the same way.
// When one or more classes use the same interface, it is referred to as "polymorphism".
// Interfaces are declared with the interface keyword
interface iMobileApp
{
// Interfaces cannot have properties
// All interface methods must be public
public function showSplashScreen();
public function login($username, $password);
public function saveData();
public function logout();
}
// To implement an interface, the implements operator is used
class AssemblerApp implements iMobileApp
{
const APPNAME = 'ASSEMBLER APP';
private $username;
private $password;
public $data;
// We have to declare every method described in the interface. If we miss a method we'll get a fatal error:
// Fatal error: Class AssemblerApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (mobileApp::methodName)
public function showSplashScreen()
{
return "¡Welcome to " . self::APPNAME . "!\n";
}
public function login($username, $password)
{
$this->username = $username;
$this->paassword = $password;
return "¡Loggin succesful with $this->username!\n";
}
public function saveData()
{
return "¡The user has successfully saved the information!\n";
}
public function logout()
{
$this->username = null;
$this->paassword = null;
return "¡The user has successfully logged out the application!\n";
}
// We can add methods not present in the interface
public function exitApp()
{
echo "Clossing " . self::APPNAME . "...";
}
}
$app = new AssemblerApp();
echo $app->showSplashScreen();
echo $app->login("XxProAssemblerxX", "123456");
echo $app->saveData();
echo $app->logout();
echo $app->exitApp();