appendices/migration74/new-features.xml
8d1a5d2d15df15cc5cbc7f96cdc083b8fe49366e
...
...
@@ -0,0 +1,406 @@
1
+
<?xml version="1.0" encoding="utf-8"?>
2
+
<!-- $Revision$ -->
3
+

4
+
<sect1 xml:id="migration74.new-features" xmlns:xlink="http://www.w3.org/1999/xlink">
5
+
<title>New Features</title>
6
+

7
+
<sect2 xml:id="migration74.new-features.core">
8
+
<title>PHP Core</title>
9
+

10
+
<sect3 xml:id="migration74.new-features.core.typed-properties">
11
+
<title>Typed properties</title>
12
+

13
+
<para>
14
+
Class properties now support type declarations.
15
+
<informalexample>
16
+
<programlisting role="php">
17
+
<![CDATA[
18
+
<?php
19
+
class User {
20
+
public int $id;
21
+
public string $name;
22
+
}
23
+
?>
24
+
]]>
25
+
</programlisting>
26
+
</informalexample>
27
+
The above example will enforce that <literal>$user->id</literal> can only be
28
+
assigned <type>int</type> values and <literal>$user->name</literal> can
29
+
only be assigned <type>string</type> values.
30
+
</para>
31
+
</sect3>
32
+

33
+
<sect3 xml:id="migration74.new-features.core.arrow-functions">
34
+
<title>Arrow functions</title>
35
+

36
+
<para>
37
+
<link linkend="functions.arrow">Arrow functions</link> provide a
38
+
shorthand syntax for defining functions
39
+
with implicit by-value scope binding.
40
+

41
+
<informalexample>
42
+
<programlisting role="php">
43
+
<![CDATA[
44
+
<?php
45
+
$factor = 10;
46
+
$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);
47
+
// $nums = array(10, 20, 30, 40);
48
+
?>
49
+
]]>
50
+
</programlisting>
51
+
</informalexample>
52
+
</para>
53
+
</sect3>
54
+

55
+
<sect3 xml:id="migration74.new-features.core.type-variance">
56
+
<title>Limited return type covariance and argument type contravariance</title>
57
+

58
+
<para>
59
+
The following code will now work:
60
+
<informalexample>
61
+
<programlisting role="php">
62
+
<![CDATA[
63
+
<?php
64
+
class A {}
65
+
class B extends A {}
66
+

67
+
class Producer {
68
+
public function method(): A {}
69
+
}
70
+
class ChildProducer extends Producer {
71
+
public function method(): B {}
72
+
}
73
+
?>
74
+
]]>
75
+
</programlisting>
76
+
</informalexample>
77
+
Full variance support is only available if autoloading is used. Inside a
78
+
single file only non-cyclic type references are possible, because all
79
+
classes need to be available before they are referenced.
80
+
</para>
81
+
</sect3>
82
+

83
+
<sect3 xml:id="migration74.new-features.core.null-coalescing-assignment-operator">
84
+
<title>Null coalescing assignment operator</title>
85
+

86
+
<para>
87
+
<informalexample>
88
+
<programlisting role="php">
89
+
<![CDATA[
90
+
<?php
91
+
$array['key'] ??= computeDefault();
92
+
// is roughly equivalent to
93
+
if (!isset($array['key'])) {
94
+
$array['key'] = computeDefault();
95
+
}
96
+
?>
97
+
]]>
98
+
</programlisting>
99
+
</informalexample>
100
+
</para>
101
+
</sect3>
102
+

103
+
<sect3 xml:id="migration74.new-features.core.unpack-inside-array">
104
+
<title>Unpacking inside arrays</title>
105
+

106
+
<para>
107
+
<informalexample>
108
+
<programlisting role="php">
109
+
<![CDATA[
110
+
<?php
111
+
$parts = ['apple', 'pear'];
112
+
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
113
+
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];
114
+
?>
115
+
]]>
116
+
</programlisting>
117
+
</informalexample>
118
+
</para>
119
+
</sect3>
120
+

121
+
<sect3 xml:id="migration74.new-features.core.numeric-literal-separator">
122
+
<title>Numeric literal separator</title>
123
+

124
+
<para>
125
+
Numeric literals can contain underscores between digits.
126
+
<informalexample>
127
+
<programlisting role="php">
128
+
<![CDATA[
129
+
<?php
130
+
6.674_083e-11; // float
131
+
299_792_458; // decimal
132
+
0xCAFE_F00D; // hexadecimal
133
+
0b0101_1111; // binary
134
+
?>
135
+
]]>
136
+
</programlisting>
137
+
</informalexample>
138
+
</para>
139
+
</sect3>
140
+
141
+
<sect3 xml:id="migration74.new-features.core.weakreference">
142
+
<title>Weak references</title>
143
+

144
+
<para>
145
+
<link linkend="class.weakreference">Weak references</link> allow the programmer to retain a reference to an object
146
+
that does not prevent the object from being destroyed.
147
+
</para>
148
+
</sect3>
149
+

150
+
<sect3 xml:id="migration74.new-features.core.tostring-exceptions">
151
+
<title>Allow exceptions from __toString()</title>
152
+

153
+
<para>
154
+
Throwing exceptions from <link linkend="object.tostring">__toString()</link>
155
+
is now permitted. Previously this resulted in a fatal error. Existing
156
+
recoverable fatal errors in string conversions have been converted to
157
+
<classname>Error</classname> exceptions.
158
+
</para>
159
+
</sect3>
160
+

161
+
</sect2>
162
+

163
+
<sect2 xml:id="migration74.new-features.curl">
164
+
<title>CURL</title>
165
+
166
+
<para>
167
+
<classname>CURLFile</classname> now supports stream wrappers in addition
168
+
to plain file names, if the extension has been built against libcurl &gt;= 7.56.0.
169
+
</para>
170
+
</sect2>
171
+

172
+
<sect2 xml:id="migration74.new-features.filter">
173
+
<title>Filter</title>
174
+
175
+
<para>
176
+
The <constant>FILTER_VALIDATE_FLOAT</constant> filter now supports the
177
+
<literal>min_range</literal> and <literal>max_range</literal>
178
+
options, with the same semantics as <constant>FILTER_VALIDATE_INT</constant>.
179
+
</para>
180
+
</sect2>
181
+

182
+
<sect2 xml:id="migration74.new-features.ffi">
183
+
<title>FFI</title>
184
+

185
+
<para>
186
+
FFI is a new extension, which provides a simple way to call
187
+
native functions, access native variables, and create/access
188
+
data structures defined in C libraries.
189
+
</para>
190
+
</sect2>
191
+

192
+
<sect2 xml:id="migration74.new-features.gd">
193
+
<title>GD</title>
194
+

195
+
<para>
196
+
Added the <constant>IMG_FILTER_SCATTER</constant> image filter
197
+
to apply a scatter filter to images.
198
+
</para>
199
+
</sect2>
200
+

201
+
<sect2 xml:id="migration74.new-features.hash">
202
+
<title>Hash</title>
203
+

204
+
<para>
205
+
Added <literal>crc32c</literal> hash using Castagnoli's polynomial.
206
+
This CRC32 variant is used by storage systems, such as
207
+
iSCSI, SCTP, Btrfs and ext4.
208
+
</para>
209
+
</sect2>
210
+

211
+
<sect2 xml:id="migration74.new-features.mbstring">
212
+
<title>Multibyte String</title>
213
+

214
+
<para>
215
+
Added the <function>mb_str_split</function> function, which provides
216
+
the same functionality as <function>str_split</function>, but operating
217
+
on code points rather than bytes.
218
+
</para>
219
+
</sect2>
220
+

221
+
<sect2 xml:id="migration74.new-features.opcache">
222
+
<title>OPcache</title>
223
+

224
+
<para>
225
+
<link linkend="opcache.preloading">Support for preloading code</link> has been added.
226
+
</para>
227
+
</sect2>
228
+

229
+
<sect2 xml:id="migration74.new-features.pcre">
230
+
<title>Regular Expressions (Perl-Compatible)</title>
231
+

232
+
<para>
233
+
The <function>preg_replace_callback</function> and <function>preg_replace_callback_array</function>
234
+
functions now accept an additional <parameter>flags</parameter> argument, with support for the
235
+
<constant>PREG_OFFSET_CAPTURE</constant> and <constant>PREG_UNMATCHED_AS_NULL</constant> flags.
236
+
This influences the format of the matches array passed to the callback function.
237
+
</para>
238
+
</sect2>
239
+

240
+
<sect2 xml:id="migration74.new-features.pdo">
241
+
<title>PDO</title>
242
+

243
+
<para>
244
+
The username and password can now be specified as part of the PDO DSN for
245
+
the mysql, mssql, sybase, dblib, firebird and oci drivers. Previously this
246
+
was only supported by the pgsql driver. If a username/password is specified
247
+
both in the constructor and the DSN, the constructor takes precedence.
248
+
</para>
249
+
<para>
250
+
It is now possible to escape question marks in SQL queries to avoid them
251
+
being interpreted as parameter placeholders. Writing <literal>??</literal>
252
+
allows sending a single question mark to the database and e.g. use the
253
+
PostgreSQL JSON key exists (<literal>?</literal>) operator.
254
+
</para>
255
+
</sect2>
256
+

257
+
<sect2 xml:id="migration74.new-features.pdo_oci">
258
+
<title>PDO_OCI</title>
259
+

260
+
<para>
261
+
<methodname>PDOStatement::getColumnMeta</methodname> is now available.
262
+
</para>
263
+
</sect2>
264
+

265
+
<sect2 xml:id="migration74.new-features.pdo_sqlite">
266
+
<title>PDO_SQLite</title>
267
+

268
+
<para>
269
+
<literal>PDOStatement::getAttribute(PDO::SQLITE_ATTR_READONLY_STATEMENT)</literal>
270
+
allows checking whether the statement is read-only, i.e. if it doesn't modify
271
+
the database.
272
+
</para>
273
+
<para>
274
+
<literal>PDO::setAttribute(PDO::SQLITE_ATTR_EXTENDED_RESULT_CODES, true)</literal>
275
+
enables the use of SQLite3 extended result codes in <function>PDO::errorInfo</function>
276
+
and <function>PDOStatement::errorInfo</function>.
277
+
</para>
278
+
</sect2>
279
+

280
+
<sect2 xml:id="migration74.new-features.sqlite3">
281
+
<title>SQLite3</title>
282
+

283
+
<para>
284
+
Added <methodname>SQLite3::lastExtendedErrorCode</methodname>
285
+
to fetch the last extended result code.
286
+
</para>
287
+
<para>
288
+
Added <literal>SQLite3::enableExtendedResultCodes($enable = true)</literal>,
289
+
which will make <methodname>SQLite3::lastErrorCode</methodname>
290
+
return extended result codes.
291
+
</para>
292
+
</sect2>
293
+

294
+
<sect2 xml:id="migration74.new-features.standard">
295
+
<title>Standard</title>
296
+

297
+
<sect3 xml:id="migration74.new-features.standard.strip-tags">
298
+
<title>strip_tags() with array of tag names</title>
299
+
<para>
300
+
<function>strip_tags</function> now also accepts an array of allowed tags:
301
+
instead of <literal>strip_tags($str, '&lt;a&gt;&lt;p&gt;')</literal>
302
+
you can now write <literal>strip_tags($str, ['a', 'p'])</literal>.
303
+
</para>
304
+
</sect3>
305
+

306
+
<sect3 xml:id="migration74.new-features.standard.magic-serialize">
307
+
<title>Custom object serialization</title>
308
+
<para>
309
+
A new mechanism for custom object serialization has been added, which
310
+
uses two new magic methods: <literal>__serialize</literal>
311
+
and <literal>__unserialize</literal>.
312
+
<informalexample>
313
+
<programlisting role="php">
314
+
<![CDATA[
315
+
<?php
316
+
// Returns array containing all the necessary state of the object.
317
+
public function __serialize(): array;
318
+

319
+
// Restores the object state from the given data array.
320
+
public function __unserialize(array $data): void;
321
+
?>
322
+
]]>
323
+
</programlisting>
324
+
</informalexample>
325
+
The new serialization mechanism supersedes the
326
+
<interfacename>Serializable</interfacename> interface,
327
+
which will be deprecated in the future.
328
+
</para>
329
+
</sect3>
330
+

331
+
<sect3 xml:id="migration74.new-features.standard.array-merge-no-args">
332
+
<title>Array merge functions without arguments</title>
333
+
<para>
334
+
<function>array_merge</function> and <function>array_merge_recursive</function>
335
+
may now be called without any arguments, in which case they will return an empty array.
336
+
This is useful in conjunction with the spread operator, e.g. <literal>array_merge(...$arrays)</literal>.
337
+
</para>
338
+
</sect3>
339
+

340
+
<sect3 xml:id="migration74.new-features.standard.proc-open">
341
+
<title><function>proc_open</function> function</title>
342
+
<para>
343
+
<function>proc_open</function> now accepts an array instead of a
344
+
string for the command. In this case the process will be opened
345
+
directly (without going through a shell) and PHP will take care of
346
+
any necessary argument escaping.
347
+
<informalexample>
348
+
<programlisting role="php">
349
+
<![CDATA[
350
+
<?php
351
+
proc_open(['php', '-r', 'echo "Hello World\n";'], $descriptors, $pipes);
352
+
?>
353
+
]]>
354
+
</programlisting>
355
+
</informalexample>
356
+
</para>
357
+
<para>
358
+
<function>proc_open</function> now supports
359
+
<literal>redirect</literal> and <literal>null</literal> descriptors.
360
+
<informalexample>
361
+
<programlisting role="php">
362
+
<![CDATA[
363
+
<?php
364
+
// Like 2>&1 on the shell
365
+
proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $pipes);
366
+
// Like 2>/dev/null or 2>nul on the shell
367
+
proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['null']], $pipes);
368
+
?>
369
+
]]>
370
+
</programlisting>
371
+
</informalexample>
372
+
</para>
373
+
</sect3>
374
+

375
+
<sect3 xml:id="migration74.new-features.standard.sodium-argon-hash">
376
+
<title>argon2i(d) without libargon</title>
377
+
<para>
378
+
<function>password_hash</function> now has the argon2i and argon2id implementations
379
+
from the sodium extension when PHP is built without libargon.
380
+
</para>
381
+
</sect3>
382
+

383
+
</sect2>
384
+

385
+
</sect1>
386
+

387
+
<!-- Keep this comment at the end of the file
388
+
Local variables:
389
+
mode: sgml
390
+
sgml-omittag:t
391
+
sgml-shorttag:t
392
+
sgml-minimize-attributes:nil
393
+
sgml-always-quote-attributes:t
394
+
sgml-indent-step:1
395
+
sgml-indent-data:t
396
+
indent-tabs-mode:nil
397
+
sgml-parent-document:nil
398
+
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
399
+
sgml-exposed-tags:nil
400
+
sgml-local-catalogs:nil
401
+
sgml-local-ecat-files:nil
402
+
End:
403
+
vim600: syn=xml fen fdm=syntax fdl=2 si
404
+
vim: et tw=78 syn=sgml
405
+
vi: ts=1 sw=1
406
+
-->
0
407