-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathinheritance.php
More file actions
78 lines (71 loc) · 2 KB
/
inheritance.php
File metadata and controls
78 lines (71 loc) · 2 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
<?php
// Example 1: -----------------------------------------------------------------
// Context:
// We have multiple user types that all share a number of methods and properties,
// instead of copying and pasting the things that these user types share, we can
// simply create a parent class that contains shared properties and function.
class User
{
// Properties
protected $username; // protected so it can be shared across child classes
protected $password;
// Methods
public function setUsername($username)
{
$this->username = $username;
}
public function getUsername()
{
return $this->username;
}
public function setPassword($pass)
{
$this->password = $pass;
}
public function login($pass)
{
if ($this->password === $pass) {
echo PHP_EOL . 'Welcome back' . PHP_EOL;
} else {
echo PHP_EOL . 'Wrong password, please try again.' . PHP_EOL;
}
}
}
class Admin extends User
{
public function whoami()
{
return 'An System Admin';
}
}
$admin = new Admin();
class Creator extends User
{
public function whoami()
{
return 'A content creator';
}
}
$creator = new Creator();
// Simulation 1
echo 'Creating new admin ... ' . PHP_EOL;
// Set username and password
$admin->setUsername('sysadmin1');
$admin->setPassword('test123');
// Test login function
echo 'New admin has been created with the name: ' . $admin->getUsername() . PHP_EOL;
echo PHP_EOL . 'Login attempt 1' . PHP_EOL;
$admin->login('test');
echo PHP_EOL . 'Login attempt 2' . PHP_EOL;
$admin->login('test123');
// Simulation 2
echo 'Creating new user ... ' . PHP_EOL;
// Set username and password
$creator->setUsername('Tami');
$creator->setPassword('test123');
// Test login function
echo 'New user has been created with the name: ' . $admin->getUsername() . PHP_EOL;
echo PHP_EOL . 'Login attempt 1' . PHP_EOL;
$creator->login('test');
echo PHP_EOL . 'Login attempt 2' . PHP_EOL;
$creator->login('test123');