language/oop5/decon.xml
9f351a14a749721a74df1dc6ac9533cf796a652e
...
...
@@ -138,7 +138,7 @@ class Point {
138
138
</programlisting>
139
139
</example>
140
140
<para>
141
-
When a constructor argument includes a visibility modifier, PHP will interpret it as
141
+
When a constructor argument includes a modifier, PHP will interpret it as
142
142
both an object property and a constructor argument, and assign the argument value to
143
143
the property. The constructor body may then be empty or may contain other statements.
144
144
Any additional statements will be executed after the argument values have been assigned
...
...
@@ -150,6 +150,13 @@ class Point {
150
150
</para>
151
151
<note>
152
152
<para>
153
+
Using a <link linkend="language.oop5.visibility">visibility modifier</link> (<literal>public</literal>,
154
+
<literal>protected</literal> or <literal>private</literal>) is the most likely way to apply property
155
+
promotion, but any other single modifier (such as <literal>readonly</literal>) will have the same effect.
156
+
</para>
157
+
</note>
158
+
<note>
159
+
<para>
153
160
Object properties may not be typed <type>callable</type> due to engine ambiguity that would
154
161
introduce. Promoted arguments, therefore, may not be typed <type>callable</type> either. Any
155
162
other <link linkend="language.types.declarations">type declaration</link> is permitted, however.
...
...
@@ -157,12 +164,66 @@ class Point {
157
164
</note>
158
165
<note>
159
166
<para>
160
-
<!-- This should be linked once attributes are documented. -->
161
-
Attributes placed on a promoted constructor argument will be replicated to both the property
162
-
and argument.
167
+
As promoted properties are desugared to both a property as well as a function parameter, any
168
+
and all naming restrictions for both properties as well as parameters apply.
169
+
</para>
170
+
</note>
171
+
<note>
172
+
<para>
173
+
<link linkend="language.attributes">Attributes</link> placed on a
174
+
promoted constructor argument will be replicated to both the property
175
+
and argument. Default values on a promoted constructor argument will be replicated only to the argument and not the property.
163
176
</para>
164
177
</note>
165
178
</sect3>
179
+

180
+
<sect3 xml:id="language.oop5.decon.constructor.new">
181
+
<title>New in initializers</title>
182
+
<para>
183
+
As of PHP 8.1.0, objects can be used as default parameter values,
184
+
static variables, and global constants, as well as in attribute arguments.
185
+
Objects can also be passed to <function>define</function> now.
186
+
</para>
187
+
<note>
188
+
<para>
189
+
The use of a dynamic or non-string class name or an anonymous class is not allowed.
190
+
The use of argument unpacking is not allowed.
191
+
The use of unsupported expressions as arguments is not allowed.
192
+
</para>
193
+
</note>
194
+
<example>
195
+
<title>Using new in initializers</title>
196
+
<programlisting role="php">
197
+
<![CDATA[
198
+
<?php
199
+

200
+
// All allowed:
201
+
static $x = new Foo;
202
+

203
+
const C = new Foo;
204
+
205
+
function test($param = new Foo) {}
206
+
207
+
#[AnAttribute(new Foo)]
208
+
class Test {
209
+
public function __construct(
210
+
public $prop = new Foo,
211
+
) {}
212
+
}
213
+

214
+
// All not allowed (compile-time error):
215
+
function test(
216
+
$a = new (CLASS_NAME_CONSTANT)(), // dynamic class name
217
+
$b = new class {}, // anonymous class
218
+
$c = new A(...[]), // argument unpacking
219
+
$d = new B($abc), // unsupported constant expression
220
+
) {}
221
+
?>
222
+
]]>
223
+
</programlisting>
224
+
</example>
225
+
</sect3>
226
+
166
227
<sect3 xml:id="language.oop5.decon.constructor.static">
167
228
<title>Static creation methods</title>
168
229
<para>
...
...
@@ -191,12 +252,12 @@ class Product {
191
252
}
192
253

193
254
public static function fromJson(string $json): static {
194
-
$data = json_decode($json);
255
+
$data = json_decode($json, true);
195
256
return new static($data['id'], $data['name']);
196
257
}
197
258

198
259
public static function fromXml(string $xml): static {
199
-
// Put your own logic here.
260
+
// Custom logic here.
200
261
$data = convert_xml_to_array($xml);
201
262
$new = new static();
202
263
$new->id = $data['id'];
203
264