language/oop5/properties.xml
f94d903985119d3ac00f4528551df947f57b667f
...
...
@@ -7,34 +7,33 @@
7
7
Class member variables are called <emphasis>properties</emphasis>.
8
8
They may be referred to using other terms such as <emphasis>fields</emphasis>,
9
9
but for the purposes of this reference <emphasis>properties</emphasis>
10
-
will be used. They are defined by using one of the keywords
11
-
<literal>public</literal>, <literal>protected</literal>, or
12
-
<literal>private</literal>, optionally, as of PHP 7.4,
10
+
will be used. They are defined by using at least one modifier (such as
11
+
<xref linkend="language.oop5.visibility"/>,
12
+
<xref linkend="language.oop5.static"/>,
13
+
or, as of PHP 8.1.0, <link linkend="language.oop5.properties.readonly-properties">readonly</link>),
14
+
optionally (except for <code>readonly</code> properties), as of PHP 7.4,
13
15
followed by a type declaration, followed by a normal variable declaration.
14
16
This declaration may include an initialization, but this initialization
15
17
must be a <link linkend="language.constants">constant</link> value.
16
18
</para>
17
-
<para>
18
-
See <xref linkend="language.oop5.visibility" /> for more
19
-
information on the meanings
20
-
of <literal>public</literal>, <literal>protected</literal>,
21
-
and <literal>private</literal>.
22
-
</para>
23
19
<note>
24
20
<para>
25
-
An alternative and not recommended way of declaring class properties, as it is to maintain backward
26
-
compatibility with PHP 4, is by using
27
-
the <literal>var</literal> keyword.
28
-
It will treat the property identically as it would have been
29
-
declared as <literal>public</literal>.
21
+
An obsolete way of declaring class properties, is by using the
22
+
<literal>var</literal> keyword instead of a modifier.
30
23
</para>
31
24
</note>
25
+
<note>
26
+
<simpara>
27
+
A property declared without a <xref linkend="language.oop5.visibility"/>
28
+
modifier will be declared as <literal>public</literal>.
29
+
</simpara>
30
+
</note>
32
31
<para>
33
32
Within class methods non-static properties may be accessed by using
34
33
<literal>-&gt;</literal> (Object Operator): <varname>$this-&gt;property</varname>
35
34
(where <literal>property</literal> is the name of the property).
36
35
Static properties are accessed by using the <literal>::</literal> (Double Colon):
37
-
<varname>self::$property</varname>. See <link linkend="language.oop5.static">Static Keyword</link>
36
+
<varname>self::$property</varname>. See <xref linkend="language.oop5.static" />
38
37
for more information on the difference between static and non-static properties.
39
38
</para>
40
39
<para>
...
...
@@ -67,6 +66,10 @@ EOD;
67
66
public $var8 = <<<'EOD'
68
67
hello world
69
68
EOD;
69
+

70
+
// Without visibility modifier:
71
+
static $var9;
72
+
readonly int $var10;
70
73
}
71
74
?>
72
75
]]>
...
...
@@ -85,7 +88,7 @@ EOD;
85
88
<sect2 xml:id="language.oop5.properties.typed-properties">
86
89
<title>Type declarations</title>
87
90
<para>
88
-
As of PHP 7.4.0, property definitions can include a
91
+
As of PHP 7.4.0, property definitions can include
89
92
<xref linkend="language.types.declarations" />,
90
93
with the exception of <type>callable</type>.
91
94
<example>
...
...
@@ -219,7 +222,7 @@ $test->prop = "foobar";
219
222
<note>
220
223
<para>
221
224
The readonly modifier can only be applied to <link linkend="language.oop5.properties.typed-properties">typed properties</link>.
222
-
A readonly property without type constraints can be created using the <xref linkend="language.types.declarations.mixed" /> type.
225
+
A readonly property without type constraints can be created using the <xref linkend="language.types.mixed"/> type.
223
226
</para>
224
227
</note>
225
228
<note>
...
...
@@ -251,7 +254,7 @@ $test1->prop = "foobar";
251
254
<note>
252
255
<para>
253
256
Specifying an explicit default value on readonly properties is not allowed, because a readonly property with a default value is essentially the same as a constant, and thus not particularly useful.
254
-
<example>
257
+
<informalexample>
255
258
<programlisting role="php">
256
259
<![CDATA[
257
260
<?php
...
...
@@ -263,7 +266,7 @@ class Test {
263
266
?>
264
267
]]>
265
268
</programlisting>
266
-
</example>
269
+
</informalexample>
267
270
</para>
268
271
</note>
269
272
<note>
...
...
@@ -273,7 +276,7 @@ class Test {
273
276
</note>
274
277
<para>
275
278
Modifications are not necessarily plain assignments, all of the following will also result in an <classname>Error</classname> exception:
276
-
<example>
279
+
<informalexample>
277
280
<programlisting role="php">
278
281
<![CDATA[
279
282
<?php
...
...
@@ -298,11 +301,11 @@ foreach ($test as &$prop);
298
301
?>
299
302
]]>
300
303
</programlisting>
301
-
</example>
304
+
</informalexample>
302
305
</para>
303
306
<para>
304
307
However, readonly properties do not preclude interior mutability. Objects (or resources) stored in readonly properties may still be modified internally:
305
-
<example>
308
+
<informalexample>
306
309
<programlisting role="php">
307
310
<![CDATA[
308
311
<?php
...
...
@@ -319,10 +322,32 @@ $test->obj = new stdClass;
319
322
?>
320
323
]]>
321
324
</programlisting>
322
-
</example>
325
+
</informalexample>
323
326
</para>
324
327
</sect2>
325
328

329
+
<sect2 xml:id="language.oop5.properties.dynamic-properties">
330
+
<title>Dynamic properties</title>
331
+
<para>
332
+
If trying to assign to a non-existent property on an &object;,
333
+
PHP will automatically create a corresponding property.
334
+
This dynamically created property will <emphasis>only</emphasis> be
335
+
available on this class instance.
336
+
</para>
337
+

338
+
<warning>
339
+
<simpara>
340
+
Dynamic properties are deprecated as of PHP 8.2.0.
341
+
It is recommended to declare the property instead.
342
+
To handle arbitrary property names, the class should implement the magic
343
+
methods <link linkend="object.get">__get()</link> and
344
+
<link linkend="object.set">__set()</link>.
345
+
At last resort the class can be marked with the
346
+
<code>#[\AllowDynamicProperties]</code> attribute.
347
+
</simpara>
348
+
</warning>
349
+
</sect2>
350
+

326
351
</sect1>
327
352
<!-- Keep this comment at the end of the file
328
353
Local variables:
329
354