-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy path09-public-private-protected.php
93 lines (79 loc) · 3.38 KB
/
09-public-private-protected.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================
/* File 09 - Access modifiers */
// Properties and methods can have access modifiers which control where they can be accessed.
// thanks to them we can improve encapsulation to our classes.
class Mobile
{
public $name; // public makes sure we can access this properties outside the class.
private $chipset; // private elements may only be accessed by the class that defines the member.
protected $internalMemory;
private $imei;
public function __construct($name, $chipset, $internalMemory, $imei)
{
$this->name = $name;
$this->chipset = $chipset;
$this->internalMemory = $internalMemory;
$this->imei = $imei;
echo "+ " . $this->name . " CREATED +<br>";
}
// now getters methods meke more sense because we won't be able to access properties outside the class
public function getName()
{
return "--- " . $this->name . " ---<br>";
}
public function getChipset()
{
return $this->chipset;
}
public function getInternalMemory()
{
return $this->internalMemory;
}
// protected elements can be accessed only within the class itself and inside inherited classes.
protected function getIMEI()
{
return $this->imei;
}
}
class Blackberry extends Mobile
{
private $keyboard;
public function __construct($name, $chipset, $internalMemory, $imei, $keyboard)
{
parent::__construct($name, $chipset, $internalMemory, $imei);
$this->keyboard = $keyboard;
}
// show protected imei
public function showIMEI()
{
return $this->getIMEI();
}
}
$samsung = new Mobile('Samsung s20', 'Exynos', 128, '000111222333');
$blackberry = new BlackBerry('BlackBerry', 'ARM', 1, '99966688555', 'qwerty');
//-----------------------------------------------------
// with this scenario we can't access private or protected
// attributes or methods via arrow operator
//-----------------------------------------------------
echo "<br>";
echo $samsung->getName(); // OK | Public method accessing a public property inside the class
echo "<br>";
echo $samsung->name; // OK | Public property
echo "<br>";
echo $samsung->getChipset(); // OK | Public method accessing a protected property inside the class
echo "<br>";
echo $samsung->chipset; // ERROR | Private property so we can't access outside the class and throws: Fatal error: Uncaught Error: Cannot access privated property Mobile::$chipset
echo "<br>";
echo $samsung->getIMEI(); // ERROR | Protected method so we can't call it outside our class and throws: Fatal error: Uncaught Error: Call to protected method Mobile::getIMEI() from context
echo "<br>";
echo $samsung->imei; // ERROR | Private property so we can't access outside the class and throws: Fatal error: Uncaught Error: Cannot access private property Mobile::$imei
echo "<br>";
echo $blackberry->showIMEI(); // OK | Public method accessing a inherited protected method inside the class
echo "<br>";
echo $blackberry->getInternalMemory(); // OK | Public method accessing a inherited protected method inside the class
echo "<br>";
echo $blackberry->internalMemory; // Uncaught Error: Cannot access protected property Blackberry::$internalMemory
echo "<br>";