-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop.php
More file actions
61 lines (53 loc) · 1.54 KB
/
Copy pathoop.php
File metadata and controls
61 lines (53 loc) · 1.54 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
<!-- Object Oriented Programming -->
<?php
/* Modifiers: public, private, protected
- public: accessible from anywhere
- private: accessible only within the class
- protected: accessible within the class and its subclasses
*/
class Person {
// Properties
public $name;
private $age;
protected $email;
public static $species;
// Constructor
public function __construct($name, $age, $email) {
$this->name = $name;
$this->age = $age;
$this->email = $email;
self::$species = "Homo sapiens";
}
// Public method
public function introduce() {
return "Hi, I'm " . $this->name . ", " . $this->getAge() . " years old." . " You can contact me at " . $this->getEmail() . ".";
}
// Private method
private function getAge() {
return $this->age;
}
// Protected method
protected function getEmail() {
return $this->email;
}
}
// Inheritance
class Employee extends Person {
private $position;
public function __construct($name, $age, $email, $position) {
parent::__construct($name, $age, $email);
$this->position = $position;
}
public function introduce() {
return parent::introduce() . " I work as a " . $this->position . ".";
}
}
// Create objects
$person = new Person("Alice", 30, "alice@example.com");
$employee = new Employee("Bob", 25, "bob@example.com", "Developer");
echo $person->introduce();
echo "<br>";
echo $employee->introduce();
echo "<br>";
echo "Species: " . Person::$species;
?>