forked from assembler-institute/oop-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-getters.php
61 lines (49 loc) · 1.45 KB
/
04-getters.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
60
61
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================
/* File 04 - Getters */
// Encapsulation is one of the principles in OOP, we use getters methods in order to keep information safe.
class Mobile
{
public $name;
public $chipset;
public $internalMemory;
// methods for getting properties
public function getName()
{
return "--- " . $this->name . " ---";
}
public function getChipset()
{
return $this->chipset;
}
public function getInternalMemory()
{
return $this->internalMemory;
}
}
$modernMobile = new Mobile();
$modernMobile->name = "Samsung s20";
$modernMobile->chipset = "Exynos";
$modernMobile->internalMemory = 128;
echo "--- GETTERS ---";
echo "<br>";
echo $modernMobile->getName();
echo "<br>";
echo $modernMobile->getChipset();
echo "<br>";
echo $modernMobile->getInternalMemory();
//-----------------------------------------------------
// with this scenario where all properties are public
// there won't be any differences between using getters
// or accessing the properties via arrow operator
//-----------------------------------------------------
echo "<br><br>";
echo "--- ARROW OPERATOR ---";
echo "<br>";
echo $modernMobile->name;
echo "<br>";
echo $modernMobile->chipset;
echo "<br>";
echo $modernMobile->internalMemory;