-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path37 OOP Access Modifiers.php
63 lines (52 loc) · 1.21 KB
/
37 OOP Access Modifiers.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
<!-- PHP OOP - Access Modifiers -->
<title>PHP OOP - Access Modifiers</title>
<!--
There are three access modifiers : -
public - The property or method can be accessed from everywhere. This is default
protected - The property or method can be accessed within the class and by classes derived from that class
private - The property or method can ONLY be accessed within the class
-->
<?php
// Define A Class
class Fruit
{
// Properties
public $name;
protected $color;
private $weight;
}
// Define An Object
$mango = new Fruit();
$mango->name = 'Mango'; // OK
$mango->color = 'Yellow'; // ERROR
$mango->weight = '300'; // ERROR
?>
<hr>
<?php
// Define A Class
class Fruits
{
// Properties
public $name;
public $color;
public $weight;
// Method
function set_name($n)
{ // a public function (default)
$this->name = $n;
}
protected function set_color($n)
{ // a protected function
$this->color = $n;
}
private function set_weight($n)
{ // a private function
$this->weight = $n;
}
}
// Define An Object
$mango = new Fruits();
$mango->set_name('Mango'); // OK
$mango->set_color('Yellow'); // ERROR
$mango->set_weight('300'); // ERROR
?>