language/oop5/properties.xml
ba65e0f4f7f28bb29b42362c38d60e41970bdfe6
...
...
@@ -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>
...
...
@@ -323,6 +326,28 @@ $test->obj = new stdClass;
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