forked from 20jam/php-oop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.php
More file actions
64 lines (58 loc) · 1.84 KB
/
inheritance.php
File metadata and controls
64 lines (58 loc) · 1.84 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
<?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 "\nWelcome back\n";
} else {
echo "\nWrong password, please try again.\n";
}
}
}
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 ... \n" . 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 "\nLogin attempt 1" . PHP_EOL;
$admin->login("test");
echo "\nLogin attempt 2" . PHP_EOL;
$admin->login("test123");
// Simulation 2
echo "Creating new user ... \n" . 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 "\nLogin attempt 1" . PHP_EOL;
$creator->login("test");
echo "\nLogin attempt 2" . PHP_EOL;
$creator->login("test123");