/
visibility.php
83 lines (71 loc) · 2.15 KB
/
visibility.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
<?php
/**
* Created by PhpStorm.
* User: php
* Date: 02-Aug-18
* Time: 8:48 PM
*/
class visibility
{
private $private = 'Private';
protected $protected = 'Protected';
public $public = 'Public';
public function visibilityFunc()
{
echo $this->private;
echo $this->protected;
echo $this->public;
}
}
//$obj = new visibility();
//echo $obj->private; // Not visible
//echo $obj->protected; // Not visible
//echo $obj->public; // Visible
//$obj->visibilityFunc(); // Visible - Values are "Protected", "Public and "Private"
class visibilityExtend extends visibility
{
// We can redeclare public and protected but not private.
public $public = 'Public extend';
public $protected = 'Protected extend';
public function visibilityFuncExtend()
{
echo $this->protected;
echo $this->public;
}
}
$objExtend = new visibilityExtend();
//echo $objExtend->public; // Visible - Value is Public extend
//echo $objExtend->protected; // Not Visible
//echo $objExtend->private; // Not Visible
//$objExtend->visibilityFuncExtend(); //Visible - Values are "Protected" and "Public extend"
/*Note: As of PHP 7.1.0, class constants may be defined as public, private, or protected. Constants declared without any explicit visibility keyword are defined as public.*/
class constantClass
{
// Here we defined the constants
public const myPublic = 'myPublic';
protected const myProtected = 'myProtected';
private const myPrivate = 'myPrivate';
public function constFunction()
{
self::myPublic;
self::myProtected;
self::myPrivate;
}
}
$obj = new constantClass();
//echo $obj::myPublic; // It works
//echo $obj::myProtected; // Fatal error
//echo $obj::myPrivate; // Fatal error
//$obj->constFunction();
class constClassExtend extends constantClass
{
public function constExtendFunction()
{
self::myPublic;
self::myProtected;
//echo self::myPrivate;
}
}
$objExtended = new constClassExtend();
//echo $objExtended::myPublic;
//$objExtended->constExtendFunction(); // myClass and myProtected works. myPrivate not works.