<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.types.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'language.types.declarations.php',
    1 => 'Type declarations',
    2 => 'Type declarations',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Типи',
  ),
  'prev' => 
  array (
    0 => 'language.types.iterable.php',
    1 => 'Iterables',
  ),
  'next' => 
  array (
    0 => 'language.types.type-juggling.php',
    1 => 'Type Juggling',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/types/declarations.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.types.declarations" class="sect1">
 <h2 class="title">Type declarations</h2>

 <p class="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 <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> is thrown.
 </p>

 <p class="para">
  Every single type that PHP supports, with the exception of
  <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> 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.
 </p>

 <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
  <p class="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
   <a href="language.oop5.variance.php" class="link">variance</a> rules.
  </p>
 </p></blockquote>

 <div class="sect2">
  <h3 class="title">Журнал змін</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Версія</th>
      <th>Опис</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.3.0</td>
      <td>
       Support for class, interface, trait, and enum constant typing has been added.
      </td>
     </tr>

     <tr>
      <td>8.2.0</td>
      <td>
       Support for <abbr title="Disjunctive Normal Form">DNF</abbr> types has been added.
      </td>
     </tr>

     <tr>
      <td>8.2.0</td>
      <td>
       Support for the literal type <span class="type"><a href="language.types.singleton.php" class="type true">true</a></span> has been added.
      </td>
     </tr>

     <tr>
      <td>8.2.0</td>
      <td>
       The types <span class="type"><a href="language.types.null.php" class="type null">null</a></span> and <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span> can now be used standalone.
      </td>
     </tr>

     <tr>
      <td>8.1.0</td>
      <td>
       Support for intersection types has been added.
      </td>
     </tr>

     <tr>
      <td>8.1.0</td>
      <td>
       Returning by reference from a <span class="type"><span class="type"><a href="language.types.void.php" class="type void">void</a></span></span> function is now deprecated.
      </td>
     </tr>

     <tr>
      <td>8.1.0</td>
      <td>
       Support for the return only type <span class="type"><a href="language.types.never.php" class="type never">never</a></span> has been added.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Support for <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> has been added.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Support for the return only type <span class="type">static</span> has been added.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Support for union types has been added.
      </td>
     </tr>

     <tr>
      <td>7.4.0</td>
      <td>
       Support for class properties typing has been added.
      </td>
     </tr>

     <tr>
      <td>7.2.0</td>
      <td>
       Support for <span class="type"><a href="language.types.object.php" class="type object">object</a></span> has been added.
      </td>
     </tr>

     <tr>
      <td>7.1.0</td>
      <td>
       Support for <span class="type"><a href="language.types.iterable.php" class="type iterable">iterable</a></span> has been added.
      </td>
     </tr>

     <tr>
      <td>7.1.0</td>
      <td>
       Support for <span class="type"><span class="type"><a href="language.types.void.php" class="type void">void</a></span></span> has been added.
      </td>
     </tr>

     <tr>
      <td>7.1.0</td>
      <td>
       Support for nullable types has been added.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>

 <div class="sect2" id="language.types.declarations.base">
  <h3 class="title">Atomic Types Usage Notes</h3>

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

  <div class="sect3" id="language.types.declarations.base.scalar">
   <h4 class="title">Scalar types</h4>
   <div class="warning"><strong class="warning">Увага</strong>
    <p class="para">
     Name aliases for scalar types (<span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>, <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>,
     <span class="type"><a href="language.types.float.php" class="type float">float</a></span>, <span class="type"><a href="language.types.string.php" class="type string">string</a></span>) are not supported.
     Instead, they are treated as class or interface names.
     For example, using <code class="literal">boolean</code> as a type declaration
     will require the value to be an <a href="language.operators.type.php" class="link"><code class="literal">instanceof</code></a> the class or interface
     <code class="literal">boolean</code>, rather than of type <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>:
    </p>
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />    </span><span style="color: #007700">function </span><span style="color: #0000BB">test</span><span style="color: #007700">(</span><span style="color: #0000BB">boolean $param</span><span style="color: #007700">) {}<br />    </span><span style="color: #0000BB">test</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

     <p class="para">В PHP 8 поданий вище приклад
виведе:</p>
     <div class="example-contents screen">
<div class="cdata"><pre>
Warning: &quot;boolean&quot; will be interpreted as a class name. Did you mean &quot;bool&quot;? Write &quot;\boolean&quot; 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
</pre></div>
     </div>
    </div>
   </div>
  </div>

  <div class="sect3" id="language.types.declarations.void">
   <h4 class="title">void</h4>
   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <p class="para">
     Returning by reference from a <span class="type"><span class="type"><a href="language.types.void.php" class="type void">void</a></span></span> function is deprecated as of PHP 8.1.0,
     because such a function is contradictory.
     Previously, it already emitted the following
     <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> when called:
     <span class="computeroutput">Only variable references should be returned by reference</span>.
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function &amp;</span><span style="color: #0000BB">test</span><span style="color: #007700">(): </span><span style="color: #0000BB">void </span><span style="color: #007700">{}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>
   </p></blockquote>
  </div>

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

   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <span class="simpara">
     It is not possible to specify the signature of the function.
    </span>
   </p></blockquote>
  </div>

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

   <p class="simpara">
    If a pass-by-reference parameter has a type declaration, the type of the
    variable is <em>only</em> 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.
   </p>
   <div class="example" id="example-1">
    <p><strong>Приклад #1 Typed pass-by-reference Parameters</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">array_baz</span><span style="color: #007700">(array &amp;</span><span style="color: #0000BB">$param</span><span style="color: #007700">)<br />{<br />    </span><span style="color: #0000BB">$param </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$var </span><span style="color: #007700">= [];<br /></span><span style="color: #0000BB">array_baz</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_baz</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад виведе щось
схоже на:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
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
</pre></div>
    </div>
   </div>
  </div>
 </div>

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

  <div class="caution"><strong class="caution">Застереження</strong>
   <p class="simpara">
    Prior to PHP 8.2.0, and the introduction of <abbr title="Disjunctive Normal Form">DNF</abbr> types,
    it was not possible to combine intersection types with union types.
   </p>
  </div>

  <div class="sect3" id="language.types.declarations.composite.union">
   <h4 class="title">Union types</h4>
   <div class="warning"><strong class="warning">Увага</strong>
    <p class="simpara">
     It is not possible to combine the two singleton types <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span>
     and <span class="type"><a href="language.types.singleton.php" class="type true">true</a></span> together in a union type.
     Use <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> instead.
    </p>
   </div>

   <div class="caution"><strong class="caution">Застереження</strong>
    <p class="simpara">
     Prior to PHP 8.2.0, as <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span> and <span class="type"><a href="language.types.null.php" class="type null">null</a></span>
     could not be used as standalone types, a union type comprised of only
     these types was not permitted. This comprises the following types:
     <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span>, <code class="literal">false|null</code>,
     and <code class="literal">?false</code>.
    </p>
   </div>

   <div class="sect4" id="language.types.declarations.nullable">
    <h5 class="title">Nullable type syntactic sugar</h5>

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

    <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
     <span class="simpara">
      This syntax is supported as of PHP 7.1.0, and predates generalized union
      types support.
     </span>
    </p></blockquote>

    <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
     <p class="para">
      It is also possible to achieve nullable arguments by making
      <code class="literal">null</code> 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
      <span class="type"><a href="language.types.null.php" class="type null">null</a></span> type will need to be added to the type declaration.
      This behavior is also deprecated since PHP 8.4.
     </p>
     <div class="example" id="example-2">
      <p><strong>Приклад #2 Old way to make arguments nullable</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">C </span><span style="color: #007700">{}<br /><br />function </span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">C $c </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">C</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

      <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
      <div class="example-contents screen">
<div class="cdata"><pre>
object(C)#1 (0) {
}
NULL
</pre></div>
      </div>
     </div>
    </p></blockquote>
   </div>
  </div>

  <div class="sect3" id="language.types.declarations.composite.redundant">
   <h4 class="title">Duplicate and redundant types</h4>
   <p class="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:

    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       Each name-resolved type may only occur once. Types such as
       <code class="literal">int|string|INT</code> or
       <code class="literal">Countable&amp;Traversable&amp;COUNTABLE</code>
       result in an error.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       Using <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> or <span class="type"><a href="language.types.never.php" class="type never">never</a></span> results in an error.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">For union types:</span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara">
         If <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> is used, <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span> or <span class="type"><a href="language.types.singleton.php" class="type true">true</a></span>
         cannot be used additionally.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         If <span class="type"><a href="language.types.object.php" class="type object">object</a></span> is used, class types cannot be used additionally.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         If <span class="type"><a href="language.types.iterable.php" class="type iterable">iterable</a></span> is used, <span class="type"><a href="language.types.array.php" class="type array">array</a></span>
         and <span class="classname"><a href="class.traversable.php" class="classname">Traversable</a></span> cannot be used additionally.
        </span>
       </li>
      </ul>
     </li>
     <li class="listitem">
      <span class="simpara">For intersection types:</span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara">
         Using a type which is not a class-type results in an error.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         Using either <span class="type">self</span>, <span class="type">parent</span>, or
         <span class="type">static</span> results in an error.
        </span>
       </li>
      </ul>
     </li>
     <li class="listitem">
      <span class="simpara">For <abbr title="Disjunctive Normal Form">DNF</abbr> types:</span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara">
         If a more generic type is used, the more restrictive one is redundant.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         Using two identical intersection types.
        </span>
       </li>
      </ul>
     </li>
    </ul>
   </p>

   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <span class="simpara">
     This does not guarantee that the type is “minimal”, because doing so would
     require loading all used class types.
    </span>
   </p></blockquote>

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

    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">int</span><span style="color: #007700">|</span><span style="color: #0000BB">INT </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Disallowed<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">bool</span><span style="color: #007700">|</span><span style="color: #0000BB">false </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Disallowed<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">int</span><span style="color: #007700">&amp;</span><span style="color: #0000BB">Traversable </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Disallowed<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">self</span><span style="color: #007700">&amp;</span><span style="color: #0000BB">Traversable </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Disallowed<br /><br /></span><span style="color: #007700">use </span><span style="color: #0000BB">A </span><span style="color: #007700">as </span><span style="color: #0000BB">B</span><span style="color: #007700">;<br />function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">A</span><span style="color: #007700">|</span><span style="color: #0000BB">B </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Disallowed ("use" is part of name resolution)<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">A</span><span style="color: #007700">&amp;</span><span style="color: #0000BB">B </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Disallowed ("use" is part of name resolution)<br /><br /></span><span style="color: #0000BB">class_alias</span><span style="color: #007700">(</span><span style="color: #DD0000">'X'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Y'</span><span style="color: #007700">);<br />function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">X</span><span style="color: #007700">|</span><span style="color: #0000BB">Y </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Allowed (redundancy is only known at runtime)<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">X</span><span style="color: #007700">&amp;</span><span style="color: #0000BB">Y </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Allowed (redundancy is only known at runtime)<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
  </div>
 </div>

 <div class="sect2" id="language.types.declarations.examples">
  <h3 class="title">Приклади</h3>
  <div class="example" id="example-3">
   <p><strong>Приклад #3 Basic class type declaration</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">C </span><span style="color: #007700">{}<br />class </span><span style="color: #0000BB">D </span><span style="color: #007700">extends </span><span style="color: #0000BB">C </span><span style="color: #007700">{}<br /><br /></span><span style="color: #FF8000">// This doesn't extend C.<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">E </span><span style="color: #007700">{}<br /><br />function </span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">C $c</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">C</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">D</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">E</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>В PHP 8 поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
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
</pre></div>
   </div>
  </div>

  <div class="example" id="example-4">
   <p><strong>Приклад #4 Basic interface type declaration</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">interface </span><span style="color: #0000BB">I </span><span style="color: #007700">{ public function </span><span style="color: #0000BB">f</span><span style="color: #007700">(); }<br />class </span><span style="color: #0000BB">C </span><span style="color: #007700">implements </span><span style="color: #0000BB">I </span><span style="color: #007700">{ public function </span><span style="color: #0000BB">f</span><span style="color: #007700">() {} }<br /><br /></span><span style="color: #FF8000">// This doesn't implement I.<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">E </span><span style="color: #007700">{}<br /><br />function </span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">I $i</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">C</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">E</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>В PHP 8 поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
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
</pre></div>
   </div>
  </div>

  <div class="example" id="example-5">
   <p><strong>Приклад #5 Basic return type declaration</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">): </span><span style="color: #0000BB">float </span><span style="color: #007700">{<br />    return </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Note that a float will be returned.<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
float(3)
</pre></div>
   </div>
  </div>

  <div class="example" id="example-6">
   <p><strong>Приклад #6 Returning an object</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">C </span><span style="color: #007700">{}<br /><br />function </span><span style="color: #0000BB">getC</span><span style="color: #007700">(): </span><span style="color: #0000BB">C </span><span style="color: #007700">{<br />    return new </span><span style="color: #0000BB">C</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">getC</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
object(C)#1 (0) {
}
</pre></div>
   </div>
  </div>

  <div class="example" id="example-7">
   <p><strong>Приклад #7 Nullable argument type declaration</strong></p>
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">C </span><span style="color: #007700">{}<br /><br />function </span><span style="color: #0000BB">f</span><span style="color: #007700">(?</span><span style="color: #0000BB">C $c</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">C</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
    <div class="cdata"><pre>
object(C)#1 (0) {
}
NULL
</pre></div>
   </div>
  </div>

  <div class="example" id="example-8">
   <p><strong>Приклад #8 Nullable return type declaration</strong></p>
   <div class="example-contents">
    <div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">get_item</span><span style="color: #007700">(): ?</span><span style="color: #0000BB">string </span><span style="color: #007700">{<br />    if (isset(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'item'</span><span style="color: #007700">])) {<br />        return </span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'item'</span><span style="color: #007700">];<br />    } else {<br />        return </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />    }<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <div class="example" id="example-9">
   <p><strong>Приклад #9 Class property type declaration</strong></p>
   <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">User </span><span style="color: #007700">{<br />    public static </span><span style="color: #0000BB">string $foo </span><span style="color: #007700">= </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;<br /><br />    public </span><span style="color: #0000BB">int $id</span><span style="color: #007700">;<br />    public </span><span style="color: #0000BB">string $username</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">int $id</span><span style="color: #007700">, </span><span style="color: #0000BB">string $username</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id </span><span style="color: #007700">= </span><span style="color: #0000BB">$id</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username </span><span style="color: #007700">= </span><span style="color: #0000BB">$username</span><span style="color: #007700">;<br />    }<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div>

  
 <div class="sect2" id="language.types.declarations.strict">
  <h3 class="title">Strict typing</h3>

  <p class="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 <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> for a parameter that expects a <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
   will get a variable of type <span class="type"><a href="language.types.string.php" class="type string">string</a></span>.
  </p>

  <p class="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 <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> will be thrown.
   The only exception to this rule is that an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> value will
   pass a <span class="type"><a href="language.types.float.php" class="type float">float</a></span> type declaration.
  </p>

  <div class="warning"><strong class="warning">Увага</strong>
   <p class="simpara">
    Function calls from within internal functions will not be affected by
    the <code class="literal">strict_types</code> declaration.
   </p>
  </div>

  <p class="para">
   To enable strict mode, the <a href="control-structures.declare.php" class="link"><code class="literal">declare</code></a> statement is used with the
   <code class="literal">strict_types</code> declaration:
  </p>

  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    Strict typing applies to function calls made from
    <em>within</em> 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&#039;s preference (coercive typing) will be
    respected, and the value will be coerced.
   </p>
  </p></blockquote>

  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    Strict typing is only defined for scalar type declarations.
   </p>
  </p></blockquote>

  <div class="example" id="example-10">
   <p><strong>Приклад #10 Strict typing for arguments values</strong></p>
   <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">strict_types</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br />function </span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">int $a</span><span style="color: #007700">, </span><span style="color: #0000BB">int $b</span><span style="color: #007700">) {<br />    return </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1.5</span><span style="color: #007700">, </span><span style="color: #0000BB">2.5</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>В PHP 8 поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
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
</pre></div>
   </div>
  </div>

  <div class="example" id="example-11">
   <p><strong>Приклад #11 Coercive typing for argument values</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">int $a</span><span style="color: #007700">, </span><span style="color: #0000BB">int $b</span><span style="color: #007700">) {<br />    return </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// These will be coerced to integers: note the output below!<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1.5</span><span style="color: #007700">, </span><span style="color: #0000BB">2.5</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
int(3)
int(3)
</pre></div>
   </div>
  </div>

  <div class="example" id="example-12">
   <p><strong>Приклад #12 Strict typing for return values</strong></p>
   <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">strict_types</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br />function </span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">): </span><span style="color: #0000BB">int </span><span style="color: #007700">{<br />    return </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2.5</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
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
</pre></div>
   </div>
  </div>
 </div>

</div><?php manual_footer($setup); ?>