language/oop5/visibility.xml
f5e5b54129045a7d02c5285a88cea0abff8ffb6f
...
...
@@ -3,22 +3,22 @@
3
3
<sect1 xml:id="language.oop5.visibility" xmlns="http://docbook.org/ns/docbook">
4
4
<title>Visibility</title>
5
5
<para>
6
-
The visibility of a property or method can be defined by prefixing
7
-
the declaration with the keywords <emphasis>public</emphasis>,
8
-
<emphasis>protected</emphasis> or
9
-
<emphasis>private</emphasis>. Class members declared public can be
6
+
The visibility of a property, a method or (as of PHP 7.1.0) a constant can be defined by prefixing
7
+
the declaration with the keywords <literal>public</literal>,
8
+
<literal>protected</literal> or
9
+
<literal>private</literal>. Class members declared public can be
10
10
accessed everywhere. Members declared protected can be accessed
11
-
only within the class itself and by inherited classes. Members
12
-
declared as private may only be accessed by the class that defines
13
-
the member.
11
+
only within the class itself and by inheriting and parent
12
+
classes. Members declared as private may only be accessed by the
13
+
class that defines the member.
14
14
</para>
15
15

16
16
<sect2 xml:id="language.oop5.visibility-members">
17
17
<title>Property Visibility</title>
18
18
<para>
19
-
Class properties must be defined as public, private, or
20
-
protected. If declared using <emphasis>var</emphasis>,
21
-
the property will be defined as public.
19
+
Class properties may be defined as public, private, or
20
+
protected. Properties declared without any explicit visibility
21
+
keyword are defined as public.
22
22
</para>
23
23
<para>
24
24
<example>
...
...
@@ -55,7 +55,8 @@ $obj->printHello(); // Shows Public, Protected and Private
55
55
*/
56
56
class MyClass2 extends MyClass
57
57
{
58
-
// We can redeclare the public and protected method, but not private
58
+
// We can redeclare the public and protected properties, but not private
59
+
public $public = 'Public2';
59
60
protected $protected = 'Protected2';
60
61

61
62
function printHello()
...
...
@@ -70,21 +71,13 @@ $obj2 = new MyClass2();
70
71
echo $obj2->public; // Works
71
72
echo $obj2->protected; // Fatal Error
72
73
echo $obj2->private; // Undefined
73
-
$obj2->printHello(); // Shows Public, Protected2, Undefined
74
+
$obj2->printHello(); // Shows Public2, Protected2, Undefined
74
75

75
76
?>
76
77
]]>
77
78
</programlisting>
78
79
</example>
79
80
</para>
80
-
<note>
81
-
<simpara>
82
-
The PHP 4 method of declaring a variable with the
83
-
<emphasis>var</emphasis> keyword is still supported for compatibility
84
-
reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its
85
-
usage would generate an <constant>E_STRICT</constant> warning.
86
-
</simpara>
87
-
</note>
88
81
</sect2>
89
82

90
83
<sect2 xml:id="language.oop5.visiblity-methods">
...
...
@@ -188,6 +181,72 @@ $myFoo->test(); // Bar::testPrivate
188
181
</para>
189
182
</sect2>
190
183

184
+
<sect2 xml:id="language.oop5.visiblity-constants">
185
+
<title>Constant Visibility</title>
186
+
<para>
187
+
As of PHP 7.1.0, class constants may be defined as public, private, or
188
+
protected. Constants declared without any explicit visibility
189
+
keyword are defined as public.
190
+
</para>
191
+
<para>
192
+
<example>
193
+
<title>Constant Declaration as of PHP 7.1.0</title>
194
+
<programlisting role="php">
195
+
<![CDATA[
196
+
<?php
197
+
/**
198
+
* Define MyClass
199
+
*/
200
+
class MyClass
201
+
{
202
+
// Declare a public constant
203
+
public const MY_PUBLIC = 'public';
204
+

205
+
// Declare a protected constant
206
+
protected const MY_PROTECTED = 'protected';
207
+

208
+
// Declare a private constant
209
+
private const MY_PRIVATE = 'private';
210
+

211
+
public function foo()
212
+
{
213
+
echo self::MY_PUBLIC;
214
+
echo self::MY_PROTECTED;
215
+
echo self::MY_PRIVATE;
216
+
}
217
+
}
218
+

219
+
$myclass = new MyClass();
220
+
MyClass::MY_PUBLIC; // Works
221
+
MyClass::MY_PROTECTED; // Fatal Error
222
+
MyClass::MY_PRIVATE; // Fatal Error
223
+
$myclass->foo(); // Public, Protected and Private work
224
+

225
+

226
+
/**
227
+
* Define MyClass2
228
+
*/
229
+
class MyClass2 extends MyClass
230
+
{
231
+
// This is public
232
+
function foo2()
233
+
{
234
+
echo self::MY_PUBLIC;
235
+
echo self::MY_PROTECTED;
236
+
echo self::MY_PRIVATE; // Fatal Error
237
+
}
238
+
}
239
+

240
+
$myclass2 = new MyClass2;
241
+
echo MyClass2::MY_PUBLIC; // Works
242
+
$myclass2->foo2(); // Public and Protected work, not Private
243
+
?>
244
+
]]>
245
+
</programlisting>
246
+
</example>
247
+
</para>
248
+
</sect2>
249
+

191
250
<sect2 xml:id="language.oop5.visibility-other-objects">
192
251
<title>Visibility from other objects</title>
193
252
<para>
...
...
@@ -242,7 +301,6 @@ Accessed the private method.
242
301
</example>
243
302
</sect2>
244
303
</sect1>
245
-
246
304
<!-- Keep this comment at the end of the file
247
305
Local variables:
248
306
mode: sgml
249
307