language/oop5/decon.xml
9f351a14a749721a74df1dc6ac9533cf796a652e
...
...
@@ -61,10 +61,10 @@ $obj = new OtherSubClass();
61
61
</programlisting>
62
62
</example>
63
63
<para>
64
-
Unlike with other methods, PHP will not generate an
65
-
<constant>E_STRICT</constant> level error message when
66
-
<link linkend="object.construct">__construct()</link> is overridden with different parameters
67
-
than the parent <link linkend="object.construct">__construct()</link> method has.
64
+
Unlike other methods, <link linkend="object.construct">__construct()</link>
65
+
is exempt from the usual
66
+
<link linkend="language.oop.lsp">signature compatibility rules</link>
67
+
when being extended.
68
68
</para>
69
69
<para>
70
70
Constructors are ordinary methods which are called during the instantiation of their
...
...
@@ -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.
176
+
</para>
177
+
</note>
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.
163
192
</para>
164
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>
165
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'];
...
...
@@ -213,10 +274,10 @@ $p3 = Product::fromXml($some_xml_string);
213
274
</example>
214
275
<para>
215
276
The constructor may be made private or protected to prevent it from being called externally.
216
-
If so, only a static method will be able to instantiate the class. Because they are in the
277
+
If so, only a static method will be able to instantiate the class. Because they are in the
217
278
same class definition they have access to private methods, even if not of the same object
218
-
instance. The private constructor is optional and may or may not make sense depending on
219
-
the use case..
279
+
instance. The private constructor is optional and may or may not make sense depending on
280
+
the use case.
220
281
</para>
221
282
<para>
222
283
The three public static methods then demonstrate different ways of instantiating the object.
...
...
@@ -243,7 +304,7 @@ $p3 = Product::fromXml($some_xml_string);
243
304
<void />
244
305
</methodsynopsis>
245
306
<para>
246
-
PHP 5 introduces a destructor concept similar to that of other
307
+
PHP possesses a destructor concept similar to that of other
247
308
object-oriented languages, such as C++. The destructor method will be
248
309
called as soon as there are no other references to a particular object,
249
310
or in any order during the shutdown sequence.
...
...
@@ -298,7 +359,6 @@ $obj = new MyDestructableClass();
298
359
</sect2>
299
360

300
361
</sect1>
301
-

302
362
<!-- Keep this comment at the end of the file
303
363
Local variables:
304
364
mode: sgml
305
365