/
new-features.xml
696 lines (601 loc) · 17.4 KB
/
new-features.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<sect1 xml:id="migration70.new-features" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>New features</title>
<sect2 xml:id="migration70.new-features.scalar-type-declarations">
<title>Scalar type declarations</title>
<para>
Scalar
<link linkend="language.types.declarations">type declarations</link>
come in two flavours: coercive (default) and strict. The following types
for parameters can now be enforced (either coercively or strictly): strings
(<type>string</type>), integers (<literal>int</literal>), floating-point
numbers (<type>float</type>), and booleans (<literal>bool</literal>). They
augment the other types introduced in PHP 5: class names, interfaces,
<type>array</type> and <type>callable</type>.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
int(9)
]]>
</screen>
</informalexample>
<para>
To enable strict mode, a single &declare; directive must be placed at the
top of the file. This means that the strictness of typing for scalars is
configured on a per-file basis. This directive not only affects the type
declarations of parameters, but also a function's return type (see
<link linkend="language.types.declarations">return type declarations</link>,
built-in PHP functions, and functions from loaded
extensions.
</para>
<para>
Full documentation and examples of scalar type declarations can be found in
the
<link linkend="language.types.declarations">type declaration</link>
reference.
</para>
</sect2>
<sect2 xml:id="migration70.new-features.return-type-declarations">
<title>Return type declarations</title>
<para>
PHP 7 adds support for
<link linkend="language.types.declarations">return type declarations</link>.
Similarly to
<link linkend="language.types.declarations">argument type declarations</link>,
return type declarations specify the type of the value that will be
returned from a function. The same
<link linkend="language.types.declarations">types</link>
are available for return type declarations as are available for argument
type declarations.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function arraysSum(array ...$arrays): array
{
return array_map(function(array $array): int {
return array_sum($array);
}, $arrays);
}
print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => 6
[1] => 15
[2] => 24
)
]]>
</screen>
</informalexample>
<para>
Full documentation and examples of return type declarations can be found in
the
<link linkend="language.types.declarations">return type declarations</link>.
reference.
</para>
</sect2>
<sect2 xml:id="migration70.new-features.null-coalesce-op">
<title>Null coalescing operator</title>
<para>
The null coalescing operator (<literal>??</literal>) has been added as
syntactic sugar for the common case of needing to use a ternary in
conjunction with <function>isset</function>. It returns its first operand
if it exists and is not &null;; otherwise it returns its second operand.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?>
]]>
</programlisting>
</informalexample>
<!-- TODO: we don't need further details here, but this still has to be
added to the operator precedence table -->
</sect2>
<sect2 xml:id="migration70.new-features.spaceship-op">
<title>Spaceship operator</title>
<para>
The spaceship operator is used for comparing two expressions. It returns -1, 0
or 1 when <varname>$a</varname> is respectively less than, equal to, or greater
than <varname>$b</varname>. Comparisons are performed according to PHP's usual
<link linkend="types.comparisons">type comparison rules</link>.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
?>
]]>
</programlisting>
</informalexample>
<!-- TODO: we don't need further details here, but this still has to be
added to the operator precedence table -->
</sect2>
<sect2 xml:id="migration70.new-features.define-array">
<title>Constant arrays using <function>define</function></title>
<para>
<type>Array</type> constants can now be defined with
<function>define</function>. In PHP 5.6, they could only be defined with
&const;.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
echo ANIMALS[1]; // outputs "cat"
?>
]]>
</programlisting>
</informalexample>
</sect2>
<sect2 xml:id="migration70.new-features.anonymous-classes">
<title>Anonymous classes</title>
<para>
Support for anonymous classes has been added via <literal>new
class</literal>. These can be used in place of full class definitions for
throwaway objects:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
interface Logger {
public function log(string $msg);
}
class Application {
private $logger;
public function getLogger(): Logger {
return $this->logger;
}
public function setLogger(Logger $logger) {
$this->logger = $logger;
}
}
$app = new Application;
$app->setLogger(new class implements Logger {
public function log(string $msg) {
echo $msg;
}
});
var_dump($app->getLogger());
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
object(class@anonymous)#2 (0) {
}
]]>
</screen>
</informalexample>
<para>
Full documentation can be found in the
<link linkend="language.oop5.anonymous">anonymous class reference</link>.
</para>
</sect2>
<sect2 xml:id="migration70.new-features.unicode-codepoint-escape-syntax">
<title>Unicode codepoint escape syntax</title>
<para>
This takes a Unicode codepoint in hexadecimal form, and outputs that
codepoint in UTF-8 to a double-quoted string or a heredoc. Any valid
codepoint is accepted, with leading 0's being optional.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
echo "\u{aa}";
echo "\u{0000aa}";
echo "\u{9999}";
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
ª
ª (same as before but with optional leading 0's)
香
]]>
</screen>
</informalexample>
</sect2>
<sect2 xml:id="migration70.new-features.closure-call-method">
<title><methodname>Closure::call</methodname></title>
<para>
<methodname>Closure::call</methodname> is a more performant, shorthand way
of temporarily binding an object scope to a closure and invoking it.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class A {private $x = 1;}
// Pre PHP 7 code
$getX = function() {return $this->x;};
$getXCB = $getX->bindTo(new A, 'A'); // intermediate closure
echo $getXCB();
// PHP 7+ code
$getX = function() {return $this->x;};
echo $getX->call(new A);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
1
1
]]>
</screen>
</informalexample>
</sect2>
<sect2 xml:id="migration70.new-features.filtered-unserialize">
<title>Filtered <function>unserialize</function></title>
<para>
This feature seeks to provide better security when unserializing objects on
untrusted data. It prevents possible code injections by enabling the
developer to whitelist classes that can be unserialized.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// converts all objects into __PHP_Incomplete_Class object
$data = unserialize($foo, ["allowed_classes" => false]);
// converts all objects into __PHP_Incomplete_Class object except those of MyClass and MyClass2
$data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]]);
// default behaviour (same as omitting the second argument) that accepts all classes
$data = unserialize($foo, ["allowed_classes" => true]);
]]>
</programlisting>
</informalexample>
</sect2>
<sect2 xml:id="migration70.new-features.intlchar">
<title><classname>IntlChar</classname></title>
<para>
The new <classname>IntlChar</classname> class seeks to expose additional
ICU functionality. The class itself defines a number of static methods and
constants that can be used to manipulate unicode characters.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
printf('%x', IntlChar::CODEPOINT_MAX);
echo IntlChar::charName('@');
var_dump(IntlChar::ispunct('!'));
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
10ffff
COMMERCIAL AT
bool(true)
]]>
</screen>
</informalexample>
<para>
In order to use this class, the <link linkend="book.intl">Intl</link> extension must be installed.
</para>
</sect2>
<sect2 xml:id="migration70.new-features.expectations">
<title>Expectations</title>
<para>
Expectations are a
backwards compatible enhancement to the older <function>assert</function>
function. They allow for zero-cost assertions in production code, and
provide the ability to throw custom exceptions when the assertion fails.
</para>
<para>
While the old API continues to be maintained for compatibility,
<function>assert</function> is now a language construct, allowing the first
parameter to be an expression rather than just a <type>string</type> to be
evaluated or a <type>bool</type> value to be tested.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
ini_set('assert.exception', 1);
class CustomError extends AssertionError {}
assert(false, new CustomError('Some error message'));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Fatal error: Uncaught CustomError: Some error message
]]>
</screen>
</informalexample>
<para>
Full details on this feature, including how to configure it in both
development and production environments, can be found on the manual page
of the <function>assert</function> language construct.
</para>
</sect2>
<sect2 xml:id="migration70.new-features.group-use-declarations">
<title>Group <literal>use</literal> declarations</title>
<para>
Classes, functions and constants being imported from the same &namespace;
can now be grouped together in a single &use.namespace; statement.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// Pre PHP 7 code
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;
use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;
use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;
// PHP 7+ code
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};
?>
]]>
</programlisting>
</informalexample>
</sect2>
<sect2 xml:id="migration70.new-features.generator-return-expressions">
<title>Generator Return Expressions</title>
<para>
This feature builds upon the generator functionality introduced into PHP 5.5.
It enables for a <literal>return</literal> statement to be used within a
generator to enable for a final expression to be returned (return by
reference is not allowed). This value can be fetched using the new
<literal>Generator::getReturn()</literal> method, which may only be used
once the generator has finished yielding values.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$gen = (function() {
yield 1;
yield 2;
return 3;
})();
foreach ($gen as $val) {
echo $val, PHP_EOL;
}
echo $gen->getReturn(), PHP_EOL;
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
1
2
3
]]>
</screen>
</informalexample>
<para>
Being able to explicitly return a final value from a generator is a handy
ability to have. This is because it enables for a final value to be returned
by a generator (from perhaps some form of coroutine computation) that can be
specifically handled by the client code executing the generator. This is far
simpler than forcing the client code to firstly check whether the final
value has been yielded, and then if so, to handle that value specifically.
</para>
</sect2>
<sect2 xml:id="migration70.new-features.generator-delegation">
<title>Generator delegation</title>
<para>
Generators can now delegate to another generator,
<classname>Traversable</classname> object or <type>array</type>
automatically, without needing to write boilerplate in the outermost
generator by using the &yield.from; construct.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function gen()
{
yield 1;
yield 2;
yield from gen2();
}
function gen2()
{
yield 3;
yield 4;
}
foreach (gen() as $val)
{
echo $val, PHP_EOL;
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
1
2
3
4
]]>
</screen>
</informalexample>
</sect2>
<sect2 xml:id="migration70.new-features.intdiv">
<title>Integer division with <function>intdiv</function></title>
<para>
The new <function>intdiv</function> function performs an integer division
of its operands and returns it.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
var_dump(intdiv(10, 3));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
int(3)
]]>
</screen>
</informalexample>
</sect2>
<sect2 xml:id="migration70.new-features.session-options">
<title>Session options</title>
<para>
<function>session_start</function> now accepts an <type>array</type> of
options that override the
<link linkend="session.configuration">session configuration directives</link>
normally set in php.ini.
</para>
<para>
These options have also been expanded to support
<link linkend="ini.session.lazy-write">session.lazy_write</link>, which is
on by default and causes PHP to only overwrite any session file if the
session data has changed, and <literal>read_and_close</literal>, which is
an option that can only be passed to <function>session_start</function> to
indicate that the session data should be read and then the session should
immediately be closed unchanged.
</para>
<para>
For example, to set
<link linkend="ini.session.cache-limiter">session.cache_limiter</link> to
<literal>private</literal> and immediately close the session after reading
it:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
session_start([
'cache_limiter' => 'private',
'read_and_close' => true,
]);
?>
]]>
</programlisting>
</informalexample>
</sect2>
<sect2 xml:id="migration70.new-features.preg-repace-callback-array-function">
<title><function>preg_replace_callback_array</function></title>
<para>
The new <function>preg_replace_callback_array</function> function enables
code to be written more cleanly when using the
<function>preg_replace_callback</function> function. Prior to PHP 7,
callbacks that needed to be executed per regular expression required the
callback function to be polluted with lots of branching.
</para>
<para>
Now, callbacks can be registered to each regular expression using an
associative array, where the key is a regular expression and the value is a
callback.
</para>
</sect2>
<sect2 xml:id="migration70.new-features.csprng-functions">
<title>CSPRNG Functions</title>
<para>
Two new functions have been added to generate cryptographically secure
integers and strings in a cross platform way:
<function>random_bytes</function> and <function>random_int</function>.
</para>
</sect2>
<sect2 xml:id="migration70.new-features.list-arrayaccess">
<title>
<function>list</function> can always unpack objects implementing
<classname>ArrayAccess</classname>
</title>
<para>
Previously, <function>list</function> was not guaranteed to operate
correctly with objects implementing <classname>ArrayAccess</classname>.
This has been fixed.
</para>
</sect2>
<sect2 xml:id="migration70.new-features.others">
<title>Other Features</title>
<itemizedlist>
<listitem>
<simpara>
Class member access on cloning has been added,
e.g. <literal>(clone $foo)->bar()</literal>.
</simpara>
</listitem>
</itemizedlist>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->