-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop.php
More file actions
63 lines (62 loc) · 2.16 KB
/
oop.php
File metadata and controls
63 lines (62 loc) · 2.16 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
<?php
//echo "OOP - Object Oriented Programming";
//From PHP5 onwards, you can define classes
class User {
//properties that belong to a class
public $name;
public $email;
public $age;
public $password;
//constructor: function that runs when an object
//is instantiated
public function __construct($name,
$email,
$age,
$password) {
//echo "constructor run<br>";
$this->name = $name;
$this->email = $email;
$this->age = $age;
$this->password = $password;
}
//method: a function that belongs to a class
function set_name($name) {
$this->name = $name;
}
//getter
function get_name() { return $this->name; }
}
//init an object
$user1 = new User('john', 'john@gmail.com', 23, '11223');
$user2 = new User('tony', 'tony@gmail.com', 19, 'abc12');
// $user1->name = 'Hoang';
// $user1->age = 43;
// $user1->email = 'sunlight4d@gmail.com';
// $user1->password = '123456';
//$user1->set_name('Hoang123');
//$user2->set_name('Bob');
// print_r($user1);
// print_r($user2);
// echo $user1->get_name()."<br>";
// echo $user2->get_name();
// echo $user2->email;
// echo $user2->name;
//inheritance
class Employee extends User {
public function __construct($name,
$email,
$age,
$password,
$title //only employee has
) {
parent::__construct($name, $email, $age, $password);
$this->title = $title;
}
public function get_title() {
return $this->title;
}
}
$employee1 = new Employee('Taylor', 'taylor12@gmail.com',
30,'123456','Sales manager');
echo $employee1->get_title();
?>