language/functions.xml
f94d903985119d3ac00f4528551df947f57b667f
...
...
@@ -175,7 +175,8 @@ function recursion($a)
175
175
<simpara>
176
176
Information may be passed to functions via the argument list,
177
177
which is a comma-delimited list of expressions. The arguments are
178
-
evaluated from left to right.
178
+
evaluated from left to right, before the function is actually called
179
+
(<emphasis>eager</emphasis> evaluation).
179
180
</simpara>
180
181

181
182
<para>
...
...
@@ -184,7 +185,8 @@ function recursion($a)
184
185
reference</link>, and <link
185
186
linkend="functions.arguments.default">default argument
186
187
values</link>. <link linkend="functions.variable-arg-list">Variable-length
187
-
argument lists</link> are also supported.
188
+
argument lists</link> and <link linkend="functions.named-arguments">Named Arguments</link>
189
+
are also supported.
188
190
</para>
189
191
<para>
190
192
<example>
...
...
@@ -225,28 +227,6 @@ function takes_many_args(
225
227
]]>
226
228
</programlisting>
227
229
</example>
228
-
<para>
229
-
As of PHP 8.0.0, passing mandatory arguments after optional arguments
230
-
is deprecated. This can generally be resolved by dropping the default value.
231
-
One exception to this rule are arguments of the form
232
-
<code>Type $param = null</code>, where the &null; default makes the type implicitly
233
-
nullable. This usage remains allowed, though it is recommended to use an
234
-
explicit nullable type instead.
235
-
</para>
236
-
<example>
237
-
<title>Passing optional arguments after mandatory arguments</title>
238
-
<programlisting role="php">
239
-
<![CDATA[
240
-
<?php
241
-
function foo($a = [], $b) {} // Before
242
-
function foo($a, $b) {} // After
243
-

244
-
function bar(A $a = null, $b) {} // Still allowed
245
-
function bar(?A $a, $b) {} // Recommended
246
-
?>
247
-
]]>
248
-
</programlisting>
249
-
</example>
250
230

251
231
<sect2 xml:id="functions.arguments.by-reference">
252
232
<title>Passing arguments by reference</title>
...
...
@@ -279,13 +259,18 @@ echo $str; // outputs 'This is a string, and something extra.'
279
259
</programlisting>
280
260
</example>
281
261
</para>
262
+
<para>
263
+
It is an error to pass a value as argument which is supposed to be passed by reference.
264
+
</para>
282
265
</sect2>
283
266
<sect2 xml:id="functions.arguments.default">
284
267
<title>Default argument values</title>
285
268
286
269
<para>
287
-
A function may define C++-style default values for scalar
288
-
arguments as follows:
270
+
A function may define default values for arguments using syntax similar
271
+
to assigning a variable. The default is used only when the parameter is
272
+
not specified; in particular, note that passing &null; does <emphasis>not</emphasis>
273
+
assign the default value.
289
274
</para>
290
275
<para>
291
276
<example>
...
...
@@ -314,8 +299,9 @@ Making a cup of espresso.
314
299
</example>
315
300
</para>
316
301
<para>
317
-
PHP also allows the use of <type>array</type>s and the special type &null;
318
-
as default values, for example:
302
+
Default parameter values may be scalar values, <type>array</type>s,
303
+
the special type &null;, and as of PHP 8.1.0, objects using the
304
+
<link linkend="language.oop5.basic.new">new ClassName()</link> syntax.
319
305
</para>
320
306
<para>
321
307
<example>
...
...
@@ -329,21 +315,61 @@ function makecoffee($types = array("cappuccino"), $coffeeMaker = NULL)
329
315
return "Making a cup of ".join(", ", $types)." with $device.\n";
330
316
}
331
317
echo makecoffee();
332
-
echo makecoffee(array("cappuccino", "lavazza"), "teapot");
333
-
?>
318
+
echo makecoffee(array("cappuccino", "lavazza"), "teapot");?>
334
319
]]>
335
320
</programlisting>
321
+
&example.outputs;
322
+
<screen>
323
+
<![CDATA[
324
+
Making a cup of cappuccino with hands.
325
+
Making a cup of cappuccino, lavazza with teapot.
326
+
]]>
327
+
</screen>
336
328
</example>
329
+
</para>
330
+
<para>
331
+
<example>
332
+
<title>Using objects as default values (as of PHP 8.1.0)</title>
333
+
<programlisting role="php">
334
+
<![CDATA[
335
+
<?php
336
+
class DefaultCoffeeMaker {
337
+
public function brew() {
338
+
return "Making coffee.\n";
339
+
}
340
+
}
341
+
class FancyCoffeeMaker {
342
+
public function brew() {
343
+
return "Crafting a beautiful coffee just for you.\n";
344
+
}
345
+
}
346
+
function makecoffee($coffeeMaker = new DefaultCoffeeMaker)
347
+
{
348
+
return $coffeeMaker->brew();
349
+
}
350
+
echo makecoffee();
351
+
echo makecoffee(new FancyCoffeeMaker);
352
+
?>
353
+
]]>
354
+
</programlisting>
337
355

356
+
&example.outputs;
357
+
<screen>
358
+
<![CDATA[
359
+
Making coffee.
360
+
Crafting a beautiful coffee just for you.
361
+
]]>
362
+
</screen>
363
+
</example>
338
364
</para>
339
365
<simpara>
340
366
The default value must be a constant expression, not (for
341
367
example) a variable, a class member or a function call.
342
368
</simpara>
343
369
<para>
344
-
Note that when using default arguments, any defaults should be on
345
-
the right side of any non-default arguments; otherwise, things
346
-
will not work as expected. Consider the following code snippet:
370
+
Note that any optional arguments should be specified after any
371
+
required arguments, otherwise they cannot be omitted from calls.
372
+
Consider the following example:
347
373
</para>
348
374
<para>
349
375
<example>
...
...
@@ -351,21 +377,20 @@ echo makecoffee(array("cappuccino", "lavazza"), "teapot");
351
377
<programlisting role="php">
352
378
<![CDATA[
353
379
<?php
354
-
function makeyogurt($type = "acidophilus", $flavour)
380
+
function makeyogurt($container = "bowl", $flavour)
355
381
{
356
-
return "Making a bowl of $type $flavour.\n";
382
+
return "Making a $container of $flavour yogurt.\n";
357
383
}
358
384
359
-
echo makeyogurt("raspberry"); // won't work as expected
385
+
echo makeyogurt("raspberry"); // "raspberry" is $container, not $flavour
360
386
?>
361
387
]]>
362
388
</programlisting>
363
389
&example.outputs;
364
390
<screen>
365
391
<![CDATA[
366
-
Warning: Missing argument 2 in call to makeyogurt() in
367
-
/usr/local/etc/httpd/htdocs/phptest/functest.html on line 41
368
-
Making a bowl of raspberry .
392
+
Fatal error: Uncaught ArgumentCountError: Too few arguments
393
+
to function makeyogurt(), 1 passed in example.php on line 42
369
394
]]>
370
395
</screen>
371
396
</example>
...
...
@@ -379,23 +404,80 @@ Making a bowl of raspberry .
379
404
<programlisting role="php">
380
405
<![CDATA[
381
406
<?php
382
-
function makeyogurt($flavour, $type = "acidophilus")
407
+
function makeyogurt($flavour, $container = "bowl")
408
+
{
409
+
return "Making a $container of $flavour yogurt.\n";
410
+
}
411
+
412
+
echo makeyogurt("raspberry"); // "raspberry" is $flavour
413
+
?>
414
+
]]>
415
+
</programlisting>
416
+
&example.outputs;
417
+
<screen>
418
+
<![CDATA[
419
+
Making a bowl of raspberry yogurt.
420
+
]]>
421
+
</screen>
422
+
</example>
423
+
</para>
424
+
<para>
425
+
As of PHP 8.0.0, <link linkend="functions.named-arguments">named arguments</link>
426
+
can be used to skip over multiple optional parameters.
427
+
</para>
428
+
<para>
429
+
<example>
430
+
<title>Correct usage of default function arguments</title>
431
+
<programlisting role="php">
432
+
<![CDATA[
433
+
<?php
434
+
function makeyogurt($container = "bowl", $flavour = "raspberry", $style = "Greek")
383
435
{
384
-
return "Making a bowl of $type $flavour.\n";
436
+
return "Making a $container of $flavour $style yogurt.\n";
385
437
}
386
438

387
-
echo makeyogurt("raspberry"); // works as expected
439
+
echo makeyogurt(style: "natural");
388
440
?>
389
441
]]>
390
442
</programlisting>
391
443
&example.outputs;
392
444
<screen>
393
445
<![CDATA[
394
-
Making a bowl of acidophilus raspberry.
446
+
Making a bowl of raspberry natural yogurt.
395
447
]]>
396
448
</screen>
397
449
</example>
398
450
</para>
451
+
<para>
452
+
As of PHP 8.0.0, declaring mandatory arguments after optional arguments
453
+
is <emphasis>deprecated</emphasis>. This can generally be resolved by
454
+
dropping the default value, since it will never be used.
455
+
One exception to this rule are arguments of the form
456
+
<code>Type $param = null</code>, where the &null; default makes the type implicitly
457
+
nullable. This usage remains allowed, though it is recommended to use an
458
+
explicit <link linkend="language.types.declarations.nullable">nullable type</link> instead.
459
+
<example>
460
+
<title>Declaring optional arguments after mandatory arguments</title>
461
+
<programlisting role="php">
462
+
<![CDATA[
463
+
<?php
464
+
function foo($a = [], $b) {} // Default not used; deprecated as of PHP 8.0.0
465
+
function foo($a, $b) {} // Functionally equivalent, no deprecation notice
466
+

467
+
function bar(A $a = null, $b) {} // Still allowed; $a is required but nullable
468
+
function bar(?A $a, $b) {} // Recommended
469
+
?>
470
+
]]>
471
+
</programlisting>
472
+
</example>
473
+
</para>
474
+
<note>
475
+
<simpara>
476
+
As of PHP 7.1.0, omitting a parameter which does not specify a default
477
+
throws an <classname>ArgumentCountError</classname>; in previous versions
478
+
it raised a Warning.
479
+
</simpara>
480
+
</note>
399
481
<note>
400
482
<simpara>
401
483
Arguments that are passed by reference may have a default value.
...
...
@@ -412,22 +494,11 @@ Making a bowl of acidophilus raspberry.
412
494
<literal>...</literal> token.
413
495
</simpara>
414
496

415
-
<note>
416
-
<simpara>
417
-
It is also possible to achieve variable-length arguments by using
418
-
<function>func_num_args</function>,
419
-
<function>func_get_arg</function>, and
420
-
<function>func_get_args</function> functions.
421
-
This technique is not recommended as it was used prior to the introduction
422
-
of the <literal>...</literal> token.
423
-
</simpara>
424
-
</note>
425
-

426
497
<para>
427
498
Argument lists may include the
428
499
<literal>...</literal> token to denote that the function accepts a
429
500
variable number of arguments. The arguments will be passed into the
430
-
given variable as an array; for example:
501
+
given variable as an &array;:
431
502

432
503
<example>
433
504
<title>Using <literal>...</literal> to access variable arguments</title>
...
...
@@ -497,7 +568,7 @@ echo add(...$a);
497
568
It is also possible to add a
498
569
<link linkend="language.types.declarations">type declaration</link> before the
499
570
<literal>...</literal> token. If this is present, then all arguments
500
-
captured by <literal>...</literal> must be objects of the hinted class.
571
+
captured by <literal>...</literal> must match that parameter type.
501
572

502
573
<example>
503
574
<title>Type declared variable arguments</title>
...
...
@@ -538,46 +609,6 @@ Catchable fatal error: Argument 2 passed to total_intervals() must be an instanc
538
609
(<literal>&amp;</literal>).
539
610
</para>
540
611

541
-
<sect3 xml:id="functions.variable-arg-list.old">
542
-
<title>Older versions of PHP</title>
543
-

544
-
<para>
545
-
No special syntax is required to note that a function is variadic;
546
-
however access to the function's arguments must use
547
-
<function>func_num_args</function>, <function>func_get_arg</function>
548
-
and <function>func_get_args</function>.
549
-
</para>
550
-

551
-
<para>
552
-
The first example above would be implemented as follows in old versions of PHP:
553
-

554
-
<example>
555
-
<title>Accessing variable arguments in old PHP versions</title>
556
-
<programlisting role="php">
557
-
<![CDATA[
558
-
<?php
559
-
function sum() {
560
-
$acc = 0;
561
-
foreach (func_get_args() as $n) {
562
-
$acc += $n;
563
-
}
564
-
return $acc;
565
-
}
566
-

567
-
echo sum(1, 2, 3, 4);
568
-
?>
569
-
]]>
570
-
</programlisting>
571
-
&example.outputs;
572
-
<screen>
573
-
<![CDATA[
574
-
10
575
-
]]>
576
-
</screen>
577
-
</example>
578
-
</para>
579
-
</sect3>
580
-

581
612
</sect2>
582
613

583
614
<sect2 xml:id="functions.named-arguments">
...
...
@@ -601,7 +632,7 @@ echo sum(1, 2, 3, 4);
601
632
<example>
602
633
<title>Named argument syntax</title>
603
634
<programlisting role="php">
604
-
<![CDATA[
635
+
<![CDATA[
605
636
<?php
606
637
myFunction(paramName: $value);
607
638
array_foobar(array: $value);
...
...
@@ -616,13 +647,13 @@ function_name($variableStoringParamName: $value);
616
647
<example>
617
648
<title>Positional arguments versus named arguments</title>
618
649
<programlisting role="php">
619
-
<![CDATA[
650
+
<![CDATA[
620
651
<?php
621
652
// Using positional arguments:
622
653
array_fill(0, 100, 50);
623
654

624
655
// Using named arguments:
625
-
array_fill(start_index: 0, num: 100, value: 50);
656
+
array_fill(start_index: 0, count: 100, value: 50);
626
657
?>
627
658
]]>
628
659
</programlisting>
...
...
@@ -635,9 +666,9 @@ array_fill(start_index: 0, num: 100, value: 50);
635
666
<example>
636
667
<title>Same example as above with a different order of parameters</title>
637
668
<programlisting role="php">
638
-
<![CDATA[
669
+
<![CDATA[
639
670
<?php
640
-
array_fill(value: 50, num: 100, start_index: 0);
671
+
array_fill(value: 50, count: 100, start_index: 0);
641
672
?>
642
673
]]>
643
674
</programlisting>
...
...
@@ -653,11 +684,11 @@ array_fill(value: 50, num: 100, start_index: 0);
653
684
<example>
654
685
<title>Combining named arguments with positional arguments</title>
655
686
<programlisting role="php">
656
-
<![CDATA[
687
+
<![CDATA[
657
688
<?php
658
689
htmlspecialchars($string, double_encode: false);
659
690
// Same as
660
-
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);
691
+
htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8', false);
661
692
?>
662
693
]]>
663
694
</programlisting>
...
...
@@ -668,9 +699,9 @@ htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);
668
699
</para>
669
700

670
701
<example>
671
-
<title>Error exception when passing the same parameter multiple times</title>
702
+
<title>Error thrown when passing the same parameter multiple times</title>
672
703
<programlisting role="php">
673
-
<![CDATA[
704
+
<![CDATA[
674
705
<?php
675
706
function foo($param) { ... }
676
707

...
...
@@ -683,6 +714,29 @@ foo(1, param: 2);
683
714
</programlisting>
684
715
</example>
685
716

717
+
<para>
718
+
As of PHP 8.1.0, it is possible to use named arguments after unpacking the arguments.
719
+
A named argument <emphasis>must not</emphasis> override an already unpacked argument.
720
+
</para>
721
+

722
+
<example>
723
+
<title>Use named arguments after unpacking</title>
724
+
<programlisting role="php">
725
+
<![CDATA[
726
+
<?php
727
+
function foo($a, $b, $c = 3, $d = 4) {
728
+
return $a + $b + $c + $d;
729
+
}
730
+

731
+
var_dump(foo(...[1, 2], d: 40)); // 46
732
+
var_dump(foo(...['b' => 2, 'a' => 1], d: 40)); // 46
733
+

734
+
var_dump(foo(...[1, 2], b: 20)); // Fatal error. Named parameter $b overwrites previous argument
735
+
?>
736
+
]]>
737
+
</programlisting>
738
+
</example>
739
+

686
740
</sect2>
687
741
</sect1>
688
742
...
...
@@ -735,9 +789,14 @@ echo square(4); // outputs '16'.
735
789
<?php
736
790
function small_numbers()
737
791
{
738
-
return array (0, 1, 2);
792
+
return [0, 1, 2];
739
793
}
740
-
list ($zero, $one, $two) = small_numbers();
794
+
// Array destructuring will collect each member of the array individually
795
+
[$zero, $one, $two] = small_numbers();
796
+

797
+
// Prior to 7.1.0, the only equivalent alternative is using list() construct
798
+
list($zero, $one, $two) = small_numbers();
799
+

741
800
?>
742
801
]]>
743
802
</programlisting>
...
...
@@ -965,8 +1024,42 @@ $func(); // prints "bar"
965
1024
the return value of the function is undefined. In this case it will
966
1025
likely return &null; but this is just a convention, and cannot be relied
967
1026
upon.
1027
+
As of PHP 8.0.0, a <classname>TypeError</classname> exception is supposed to
1028
+
be thrown in this case.
968
1029
</simpara>
969
1030
</note>
1031
+
<note>
1032
+
<para>
1033
+
Scalar types for built-in functions are nullable by default in coercive mode.
1034
+
As of PHP 8.1.0, passing &null; to an internal function parameter that is not declared nullable
1035
+
is discouraged and emits a deprecation notice in coercive mode to align with the behavior of user-defined functions,
1036
+
where scalar types need to be marked as nullable explicitly.
1037
+
</para>
1038
+

1039
+
<para>
1040
+
For example, <function>strlen</function> function expects the parameter <literal>$string</literal>
1041
+
to be a non-nullable &string;.
1042
+
For historical reasons, PHP allows passing &null; for this parameter in coercive mode, and the parameter is
1043
+
implicitly cast to <type>string</type>, resulting in a <literal>""</literal> value.
1044
+
In contrast, a <classname>TypeError</classname> is emitted in strict mode.
1045
+
</para>
1046
+

1047
+
<informalexample>
1048
+
<programlisting role="php">
1049
+
<![CDATA[
1050
+
<?php
1051
+
var_dump(strlen(null));
1052
+
// "Deprecated: Passing null to parameter #1 ($string) of type string is deprecated" as of PHP 8.1.0
1053
+
// int(0)
1054
+

1055
+
var_dump(str_contains("foobar", null));
1056
+
// "Deprecated: Passing null to parameter #2 ($needle) of type string is deprecated" as of PHP 8.1.0
1057
+
// bool(true)
1058
+
?>
1059
+
]]>
1060
+
</programlisting>
1061
+
</informalexample>
1062
+
</note>
970
1063

971
1064
<sect2 role="seealso">
972
1065
&reftitle.seealso;
...
...
@@ -1022,8 +1115,7 @@ echo preg_replace_callback('~-([a-z])~', function ($match) {
1022
1115
<programlisting role="php">
1023
1116
<![CDATA[
1024
1117
<?php
1025
-
$greet = function($name)
1026
-
{
1118
+
$greet = function($name) {
1027
1119
printf("Hello %s\r\n", $name);
1028
1120
};
1029
1121

...
...
@@ -1039,6 +1131,8 @@ $greet('PHP');
1039
1131
variables must be passed to the <literal>use</literal> language construct.
1040
1132
As of PHP 7.1, these variables must not include &link.superglobals;,
1041
1133
<varname>$this</varname>, or variables with the same name as a parameter.
1134
+
A return type declaration of the function has to be placed
1135
+
<emphasis>after</emphasis> the <literal>use</literal> clause.
1042
1136
</simpara>
1043
1137

1044
1138
<example>
...
...
@@ -1084,6 +1178,12 @@ $example = function ($arg) use ($message) {
1084
1178
var_dump($arg . ' ' . $message);
1085
1179
};
1086
1180
$example("hello");
1181
+

1182
+
// Return type declaration comes after the use clause
1183
+
$example = function () use ($message): string {
1184
+
return "hello $message";
1185
+
};
1186
+
var_dump($example());
1087
1187
?>
1088
1188
]]>
1089
1189
</programlisting>
...
...
@@ -1097,6 +1197,7 @@ string(5) "hello"
1097
1197
string(5) "hello"
1098
1198
string(5) "world"
1099
1199
string(11) "hello world"
1200
+
string(11) "hello world"
1100
1201
]]>
1101
1202
</screen>
1102
1203
</example>
...
...
@@ -1264,7 +1365,7 @@ NULL
1264
1365
$func = static function() {
1265
1366
// function body
1266
1367
};
1267
-
$func = $func->bindTo(new StdClass);
1368
+
$func = $func->bindTo(new stdClass);
1268
1369
$func();
1269
1370

1270
1371
?>
...
...
@@ -1483,6 +1584,127 @@ var_export($x); // Outputs 1
1483
1584
</sect2>
1484
1585
</sect1>
1485
1586

1587
+
<sect1 xml:id="functions.first_class_callable_syntax">
1588
+
<title>First class callable syntax</title>
1589
+

1590
+
<para>
1591
+
The first class callable syntax is introduced as of PHP 8.1.0, as a way of creating <link linkend="functions.anonymous">anonymous functions</link> from <link linkend="language.types.callable">callable</link>.
1592
+
It supersedes existing callable syntax using strings and arrays.
1593
+
The advantage of this syntax is that it is accessible to static analysis, and uses the scope at the point where the callable is acquired.
1594
+
</para>
1595
+

1596
+
<para>
1597
+
<code>CallableExpr(...)</code> syntax is used to create a <classname>Closure</classname> object from callable. <code>CallableExpr</code> accepts any expression that can be directly called in the PHP grammar:
1598
+
<example>
1599
+
<title>Simple first class callable syntax</title>
1600
+
<programlisting role="php">
1601
+
<![CDATA[
1602
+
<?php
1603
+

1604
+
class Foo {
1605
+
public function method() {}
1606
+
public static function staticmethod() {}
1607
+
public function __invoke() {}
1608
+
}
1609
+

1610
+
$obj = new Foo();
1611
+
$classStr = 'Foo';
1612
+
$methodStr = 'method';
1613
+
$staticmethodStr = 'staticmethod';
1614
+

1615
+

1616
+
$f1 = strlen(...);
1617
+
$f2 = $obj(...); // invokable object
1618
+
$f3 = $obj->method(...);
1619
+
$f4 = $obj->$methodStr(...);
1620
+
$f5 = Foo::staticmethod(...);
1621
+
$f6 = $classStr::$staticmethodStr(...);
1622
+

1623
+
// traditional callable using string, array
1624
+
$f7 = 'strlen'(...);
1625
+
$f8 = [$obj, 'method'](...);
1626
+
$f9 = [Foo::class, 'staticmethod'](...);
1627
+
?>
1628
+
]]>
1629
+
</programlisting>
1630
+
</example>
1631
+
</para>
1632
+

1633
+
<note>
1634
+
<para>
1635
+
The <code>...</code> is part of the syntax, and not an omission.
1636
+
</para>
1637
+
</note>
1638
+

1639
+
<para>
1640
+
<code>CallableExpr(...)</code> has the same semantics as <methodname>Closure::fromCallable</methodname>.
1641
+
That is, unlike callable using strings and arrays, <code>CallableExpr(...)</code> respects the scope at the point where it is created:
1642
+
<example>
1643
+
<title>Scope comparison of <code>CallableExpr(...)</code> and traditional callable</title>
1644
+
<programlisting role="php">
1645
+
<![CDATA[
1646
+
<?php
1647
+

1648
+
class Foo {
1649
+
public function getPrivateMethod() {
1650
+
return [$this, 'privateMethod'];
1651
+
}
1652
+

1653
+
private function privateMethod() {
1654
+
echo __METHOD__, "\n";
1655
+
}
1656
+
}
1657
+

1658
+
$foo = new Foo;
1659
+
$privateMethod = $foo->getPrivateMethod();
1660
+
$privateMethod();
1661
+
// Fatal error: Call to private method Foo::privateMethod() from global scope
1662
+
// This is because call is performed outside from Foo and visibility will be checked from this point.
1663
+

1664
+
class Foo1 {
1665
+
public function getPrivateMethod() {
1666
+
// Uses the scope where the callable is acquired.
1667
+
return $this->privateMethod(...); // identical to Closure::fromCallable([$this, 'privateMethod']);
1668
+
}
1669
+

1670
+
private function privateMethod() {
1671
+
echo __METHOD__, "\n";
1672
+
}
1673
+
}
1674
+

1675
+
$foo1 = new Foo1;
1676
+
$privateMethod = $foo1->getPrivateMethod();
1677
+
$privateMethod(); // Foo1::privateMethod
1678
+
?>
1679
+
]]>
1680
+
</programlisting>
1681
+
</example>
1682
+

1683
+
</para>
1684
+

1685
+
<note>
1686
+
<para>
1687
+
Object creation by this syntax (e.g <code>new Foo(...)</code>) is not supported, because <code>new Foo()</code> syntax is not considered a call.
1688
+
</para>
1689
+
</note>
1690
+

1691
+
<note>
1692
+
<para>
1693
+
The first-class callable syntax cannot be combined with the <link linkend="language.oop5.basic.nullsafe">nullsafe operator</link>. Both of the following result in a compile-time error:
1694
+
<informalexample>
1695
+
<programlisting role="php">
1696
+
<![CDATA[
1697
+
<?php
1698
+
$obj?->method(...);
1699
+
$obj?->prop->method(...);
1700
+
?>
1701
+
]]>
1702
+
</programlisting>
1703
+
</informalexample>
1704
+
</para>
1705
+
</note>
1706
+
</sect1>
1707
+

1486
1708
</chapter>
1487
1709
1488
1710
<!-- Keep this comment at the end of the file
1489
1711