-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17_oop.php
59 lines (46 loc) · 1.54 KB
/
17_oop.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
<?php
/* --- Object Oriented Programming -- */
/*
From PHP5 onwards you can write PHP in either a procedural or object oriented way. OOP consists of classes that can hold "properties" and "methods". Objects can be created from classes.
*/
class User {
// Properties
// Access modifiers
// public: can be accessed from outside the class
// protected: can be accessed only within the class itself and by inherited and parent classes
// private: can only be accessed within the class that defines the property
public $name;
public $email;
public $password;
// A constructor is a method that runs when an object is created
public function __construct($name, $email, $password) {
$this->name = $name;
$this->email = $email;
$this->password = $password;
}
// Methods - functions that belong to a class
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
// Instantiate a User object
$user1 = new User('John Doe', 'johndoe@gmail.com', '123456');
$user2 = new User('Stefan Gogov', 'email@gmail.com', '123456');
echo $user1->name . '<br>';
echo $user2->name . '<br>';
// Inheritance
class Employee extends User {
public $position;
public function __construct($name, $email, $password, $position) {
parent::__construct($name, $email, $password);
$this->position = $position;
}
public function get_position() {
return $this->position;
}
}
$employee1 = new Employee('Sara', 'sara@gmail.com', '2314214', 'Manager');
echo $employee1->get_position() . '<br>';