File: declarations.xml

package info (click to toggle)
php-doc 20241205~git.dfcbb86%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 70,956 kB
  • sloc: xml: 968,269; php: 23,883; javascript: 671; sh: 177; makefile: 37
file content (797 lines) | stat: -rw-r--r-- 19,250 bytes parent folder | download
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<sect1 xml:id="language.types.declarations">
 <title>Type declarations</title>

 <para>
  Type declarations can be added to function arguments, return values,
  as of PHP 7.4.0, class properties, and as of PHP 8.3.0, class constants.
  They ensure that the value is of the specified type at call time,
  otherwise a <classname>TypeError</classname> is thrown.
 </para>

 <para>
  Every single type that PHP supports, with the exception of
  <type>resource</type> can be used within a user-land type declaration.
  This page contains a changelog of availability of the different types
  and documentation about usage of them in type declarations.
 </para>

 <note>
  <para>
   When a class implements an interface method or reimplements a method which
   has already been defined by a parent class, it has to be compatible with the
   aforementioned definition.
   A method is compatible if it follows the
   <link linkend="language.oop5.variance">variance</link> rules.
  </para>
 </note>

 <sect2 role="changelog">
  &reftitle.changelog;
  <informaltable>
   <tgroup cols="2">
    <thead>
     <row>
      <entry>&Version;</entry>
      <entry>&Description;</entry>
     </row>
    </thead>
    <tbody>
     <row>
      <entry>8.3.0</entry>
      <entry>
       Support for class, interface, trait, and enum constant typing has been added.
      </entry>
     </row>
     <row>
      <entry>8.2.0</entry>
      <entry>
       Support for <acronym>DNF</acronym> types has been added.
      </entry>
     </row>
     <row>
      <entry>8.2.0</entry>
      <entry>
       Support for the literal type <type>true</type> has been added.
      </entry>
     </row>
     <row>
      <entry>8.2.0</entry>
      <entry>
       The types <type>null</type> and <type>false</type> can now be used standalone.
      </entry>
     </row>
     <row>
      <entry>8.1.0</entry>
      <entry>
       Support for intersection types has been added.
      </entry>
     </row>
     <row>
      <entry>8.1.0</entry>
      <entry>
       Returning by reference from a <type>void</type> function is now deprecated.
      </entry>
     </row>
     <row>
      <entry>8.1.0</entry>
      <entry>
       Support for the return only type <type>never</type> has been added.
      </entry>
     </row>
     <row>
      <entry>8.0.0</entry>
      <entry>
       Support for <type>mixed</type> has been added.
      </entry>
     </row>
     <row>
      <entry>8.0.0</entry>
      <entry>
       Support for the return only type <type>static</type> has been added.
      </entry>
     </row>
     <row>
      <entry>8.0.0</entry>
      <entry>
       Support for union types has been added.
      </entry>
     </row>
     <row>
      <entry>7.4.0</entry>
      <entry>
       Support for class properties typing has been added.
      </entry>
     </row>
     <row>
      <entry>7.2.0</entry>
      <entry>
       Support for <type>object</type> has been added.
      </entry>
     </row>
     <row>
      <entry>7.1.0</entry>
      <entry>
       Support for <type>iterable</type> has been added.
      </entry>
     </row>
     <row>
      <entry>7.1.0</entry>
      <entry>
       Support for <type>void</type> has been added.
      </entry>
     </row>
     <row>
      <entry>7.1.0</entry>
      <entry>
       Support for nullable types has been added.
      </entry>
     </row>
    </tbody>
   </tgroup>
  </informaltable>
 </sect2>

 <sect2 xml:id="language.types.declarations.base">
  <title>Atomic Types Usage Notes</title>

  <simpara>
   Atomic types have straight forward behaviour with some minor caveats which
   are described in this section.
  </simpara>

  <sect3 xml:id="language.types.declarations.base.scalar">
   <title>Scalar types</title>
   <warning>
    <para>
     Name aliases for scalar types (<type>bool</type>, <type>int</type>,
     <type>float</type>, <type>string</type>) are not supported.
     Instead, they are treated as class or interface names.
     For example, using <literal>boolean</literal> as a type declaration
     will require the value to be an &instanceof; the class or interface
     <literal>boolean</literal>, rather than of type <type>bool</type>:
    </para>
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
    function test(boolean $param) {}
    test(true);
?>
]]>
     </programlisting>
     &example.outputs.8;
     <screen>
<![CDATA[
Warning: "boolean" will be interpreted as a class name. Did you mean "bool"? Write "\boolean" to suppress this warning in /in/9YrUX on line 2

Fatal error: Uncaught TypeError: test(): Argument #1 ($param) must be of type boolean, bool given, called in - on line 3 and defined in -:2
Stack trace:
#0 -(3): test(true)
#1 {main}
  thrown in - on line 2
]]>
     </screen>
    </informalexample>
   </warning>
  </sect3>

  <sect3 xml:id="language.types.declarations.void">
   <title>void</title>
   <note>
    <para>
     Returning by reference from a <type>void</type> function is deprecated as of PHP 8.1.0,
     because such a function is contradictory.
     Previously, it already emitted the following
     <constant>E_NOTICE</constant> when called:
     <computeroutput>Only variable references should be returned by reference</computeroutput>.
     <informalexample>
      <programlisting role="php">
<![CDATA[
<?php
function &test(): void {}
?>
]]>
      </programlisting>
     </informalexample>
    </para>
   </note>
  </sect3>

  <sect3 xml:id="language.types.declarations.base.function">
   <title>Callable types</title>
   <para>
    This type cannot be used as a class property type declaration.
   </para>

   <note>
    <simpara>
     It is not possible to specify the signature of the function.
    </simpara>
   </note>
  </sect3>

  <sect3 xml:id="language.types.declarations.references">
   <title>Type declarations on pass-by-reference Parameters</title>

   <simpara>
    If a pass-by-reference parameter has a type declaration, the type of the
    variable is <emphasis>only</emphasis> checked on function entry, at the
    beginning of the call, but not when the function returns.
    This means that a function can change the type of variable reference.
   </simpara>
   <example>
    <title>Typed pass-by-reference Parameters</title>
    <programlisting role="php">
<![CDATA[
<?php
function array_baz(array &$param)
{
    $param = 1;
}
$var = [];
array_baz($var);
var_dump($var);
array_baz($var);
?>
]]>
    </programlisting>
    &example.outputs.similar;
    <screen>
<![CDATA[
int(1)

Fatal error: Uncaught TypeError: array_baz(): Argument #1 ($param) must be of type array, int given, called in - on line 9 and defined in -:2
Stack trace:
#0 -(9): array_baz(1)
#1 {main}
  thrown in - on line 2
]]>
    </screen>
   </example>
  </sect3>
 </sect2>

 <sect2 xml:id="language.types.declarations.composite">
  <title>Composite Types Usage Notes</title>
  <para>
   Composite type declarations are subject to a couple of restrictions and
   will perform a redundancy check at compile time to prevent simple bugs.
  </para>

  <caution>
   <simpara>
    Prior to PHP 8.2.0, and the introduction of <acronym>DNF</acronym> types,
    it was not possible to combine intersection types with union types.
   </simpara>
  </caution>

  <sect3 xml:id="language.types.declarations.composite.union">
   <title>Union types</title>
   <warning>
    <simpara>
     It is not possible to combine the two value types <type>false</type>
     and <type>true</type> together in a union type.
     Use <type>bool</type> instead.
    </simpara>
   </warning>

   <caution>
    <simpara>
     Prior to PHP 8.2.0, as <type>false</type> and <type>null</type>
     could not be used as standalone types, a union type comprised of only
     these types was not permitted. This comprises the following types:
     <type>false</type>, <literal>false|null</literal>,
     and <literal>?false</literal>.
    </simpara>
   </caution>

   <sect4 xml:id="language.types.declarations.nullable">
    <title>Nullable type syntactic sugar</title>

    <para>
     A single base type declaration can be marked nullable by prefixing the
     type with a question mark (<literal>?</literal>).
     Thus <literal>?T</literal> and <literal>T|null</literal> are identical.
    </para>

    <note>
     <simpara>
      This syntax is supported as of PHP 7.1.0, and predates generalized union
      types support.
     </simpara>
    </note>

    <note>
     <para>
      It is also possible to achieve nullable arguments by making
      <literal>null</literal> the default value.
      This is not recommended as if the default value is changed in a child
      class a type compatibility violation will be raised as the
      <type>null</type> type will need to be added to the type declaration.
     </para>
     <example>
      <title>Old way to make arguments nullable</title>
      <programlisting role="php">
<![CDATA[
<?php
class C {}

function f(C $c = null) {
    var_dump($c);
}

f(new C);
f(null);
?>
]]>
      </programlisting>
      &example.outputs;
      <screen>
<![CDATA[
object(C)#1 (0) {
}
NULL
]]>
      </screen>
     </example>
    </note>
   </sect4>
  </sect3>

  <sect3 xml:id="language.types.declarations.composite.redundant">
   <title>Duplicate and redundant types</title>
   <para>
    To catch simple bugs in composite type declarations, redundant types that
    can be detected without performing class loading will result in a
    compile-time error. This includes:

    <itemizedlist>
     <listitem>
      <simpara>
       Each name-resolved type may only occur once. Types such as
       <literal>int|string|INT</literal> or
       <literal>Countable&amp;Traversable&amp;COUNTABLE</literal>
       result in an error.
      </simpara>
     </listitem>
     <listitem>
      <simpara>
       Using <type>mixed</type> results in an error.
      </simpara>
     </listitem>
     <listitem>
      <simpara>For union types:</simpara>
      <itemizedlist>
       <listitem>
        <simpara>
         If <type>bool</type> is used, <type>false</type> or <type>true</type>
         cannot be used additionally.
        </simpara>
       </listitem>
       <listitem>
        <simpara>
         If <type>object</type> is used, class types cannot be used additionally.
        </simpara>
       </listitem>
       <listitem>
        <simpara>
         If <type>iterable</type> is used, <type>array</type>
         and <classname>Traversable</classname> cannot be used additionally.
        </simpara>
       </listitem>
      </itemizedlist>
     </listitem>
     <listitem>
      <simpara>For intersection types:</simpara>
      <itemizedlist>
       <listitem>
        <simpara>
         Using a type which is not a class-type results in an error.
        </simpara>
       </listitem>
       <listitem>
        <simpara>
         Using either <type>self</type>, <type>parent</type>, or
         <type>static</type> results in an error.
        </simpara>
       </listitem>
      </itemizedlist>
     </listitem>
     <listitem>
      <simpara>For <acronym>DNF</acronym> types:</simpara>
      <itemizedlist>
       <listitem>
        <simpara>
         If a more generic type is used, the more restrictive one is redundant.
        </simpara>
       </listitem>
       <listitem>
        <simpara>
         Using two identical intersection types.
        </simpara>
       </listitem>
      </itemizedlist>
     </listitem>
    </itemizedlist>
   </para>

   <note>
    <simpara>
     This does not guarantee that the type is “minimal”, because doing so would
     require loading all used class types.
    </simpara>
   </note>

   <para>
    For example, if <literal>A</literal> and <literal>B</literal> are class
    aliases, then <literal>A|B</literal> remains a legal union type, even
    though it could be reduced to either <literal>A</literal> or
    <literal>B</literal>.
    Similarly, if class <code>B extends A {}</code>, then <literal>A|B</literal>
    is also a legal union type, even though it could be reduced to just
    <literal>A</literal>.

    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
function foo(): int|INT {} // Disallowed
function foo(): bool|false {} // Disallowed
function foo(): int&Traversable {} // Disallowed
function foo(): self&Traversable {} // Disallowed

use A as B;
function foo(): A|B {} // Disallowed ("use" is part of name resolution)
function foo(): A&B {} // Disallowed ("use" is part of name resolution)

class_alias('X', 'Y');
function foo(): X|Y {} // Allowed (redundancy is only known at runtime)
function foo(): X&Y {} // Allowed (redundancy is only known at runtime)
?>
]]>
     </programlisting>
    </informalexample>
   </para>
  </sect3>
 </sect2>

 <sect2 xml:id="language.types.declarations.examples">
  &reftitle.examples;
  <example>
   <title>Basic class type declaration</title>
   <programlisting role="php">
<![CDATA[
<?php
class C {}
class D extends C {}

// This doesn't extend C.
class E {}

function f(C $c) {
    echo get_class($c)."\n";
}

f(new C);
f(new D);
f(new E);
?>
]]>
   </programlisting>
   &example.outputs.8;
   <screen>
<![CDATA[
C
D

Fatal error: Uncaught TypeError: f(): Argument #1 ($c) must be of type C, E given, called in /in/gLonb on line 14 and defined in /in/gLonb:8
Stack trace:
#0 -(14): f(Object(E))
#1 {main}
  thrown in - on line 8
]]>
   </screen>
  </example>

  <example>
   <title>Basic interface type declaration</title>
   <programlisting role="php">
<![CDATA[
<?php
interface I { public function f(); }
class C implements I { public function f() {} }

// This doesn't implement I.
class E {}

function f(I $i) {
    echo get_class($i)."\n";
}

f(new C);
f(new E);
?>
]]>
   </programlisting>
   &example.outputs.8;
   <screen>
<![CDATA[
C

Fatal error: Uncaught TypeError: f(): Argument #1 ($i) must be of type I, E given, called in - on line 13 and defined in -:8
Stack trace:
#0 -(13): f(Object(E))
#1 {main}
  thrown in - on line 8
]]>
   </screen>
  </example>

  <example>
   <title>Basic return type declaration</title>
   <programlisting role="php">
<![CDATA[
<?php
function sum($a, $b): float {
    return $a + $b;
}

// Note that a float will be returned.
var_dump(sum(1, 2));
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
float(3)
]]>
   </screen>
  </example>

  <example>
   <title>Returning an object</title>
   <programlisting role="php">
<![CDATA[
<?php
class C {}

function getC(): C {
    return new C;
}

var_dump(getC());
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
object(C)#1 (0) {
}
]]>
   </screen>
  </example>

  <example>
   <title>Nullable argument type declaration</title>
   <programlisting role="php">
    <![CDATA[
<?php
class C {}

function f(?C $c) {
    var_dump($c);
}

f(new C);
f(null);
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
    <![CDATA[
object(C)#1 (0) {
}
NULL
]]>
   </screen>
  </example>

  <example>
   <title>Nullable return type declaration</title>
   <programlisting role="php">
    <![CDATA[
<?php
function get_item(): ?string {
    if (isset($_GET['item'])) {
        return $_GET['item'];
    } else {
        return null;
    }
}
?>
]]>
   </programlisting>
  </example>

  <example>
   <title>Class property type declaration</title>
   <programlisting role="php">
<![CDATA[
<?php
class User {
    public static string $foo = 'foo';

    public int $id;
    public string $username;

    public function __construct(int $id, string $username) {
        $this->id = $id;
        $this->username = $username;
    }
}
?>
]]>
   </programlisting>
  </example>
 </sect2>

  <!-- TODO Move this into its own declare page -->
 <sect2 xml:id="language.types.declarations.strict">
  <title>Strict typing</title>

  <para>
   By default, PHP will coerce values of the wrong type into the expected
   scalar type declaration if possible. For example, a function that is given
   an <type>int</type> for a parameter that expects a <type>string</type>
   will get a variable of type <type>string</type>.
  </para>

  <para>
   It is possible to enable strict mode on a per-file basis. In strict
   mode, only a value corresponding exactly to the type declaration will be
   accepted, otherwise a <classname>TypeError</classname> will be thrown.
   The only exception to this rule is that an <type>int</type> value will
   pass a <type>float</type> type declaration.
  </para>

  <warning>
   <simpara>
    Function calls from within internal functions will not be affected by
    the <literal>strict_types</literal> declaration.
   </simpara>
  </warning>

  <para>
   To enable strict mode, the &declare; statement is used with the
   <literal>strict_types</literal> declaration:
  </para>

  <note>
   <para>
    Strict typing applies to function calls made from
    <emphasis>within</emphasis> the file with strict typing enabled, not to
    the functions declared within that file. If a file without strict
    typing enabled makes a call to a function that was defined in a file
    with strict typing, the caller's preference (coercive typing) will be
    respected, and the value will be coerced.
   </para>
  </note>

  <note>
   <para>
    Strict typing is only defined for scalar type declarations.
   </para>
  </note>

  <example>
   <title>Strict typing for arguments values</title>
   <programlisting role="php">
<![CDATA[
<?php
declare(strict_types=1);

function sum(int $a, int $b) {
    return $a + $b;
}

var_dump(sum(1, 2));
var_dump(sum(1.5, 2.5));
?>
]]>
   </programlisting>
   &example.outputs.8;
   <screen>
<![CDATA[
int(3)

Fatal error: Uncaught TypeError: sum(): Argument #1 ($a) must be of type int, float given, called in - on line 9 and defined in -:4
Stack trace:
#0 -(9): sum(1.5, 2.5)
#1 {main}
  thrown in - on line 4
]]>
   </screen>
  </example>

  <example>
   <title>Coercive typing for argument values</title>
   <programlisting role="php">
<![CDATA[
<?php
function sum(int $a, int $b) {
    return $a + $b;
}

var_dump(sum(1, 2));

// These will be coerced to integers: note the output below!
var_dump(sum(1.5, 2.5));
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
int(3)
int(3)
]]>
   </screen>
  </example>

  <example>
   <title>Strict typing for return values</title>
   <programlisting role="php">
<![CDATA[
<?php
declare(strict_types=1);

function sum($a, $b): int {
    return $a + $b;
}

var_dump(sum(1, 2));
var_dump(sum(1, 2.5));
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
int(3)

Fatal error: Uncaught TypeError: sum(): Return value must be of type int, float returned in -:5
Stack trace:
#0 -(9): sum(1, 2.5)
#1 {main}
  thrown in - on line 5
]]>
   </screen>
  </example>
 </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
-->