<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'language.types.declarations.php',
    1 => 'D&eacute;clarations de type',
    2 => 'D&eacute;clarations de type',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Les types',
  ),
  'prev' => 
  array (
    0 => 'language.types.iterable.php',
    1 => 'It&eacute;rables',
  ),
  'next' => 
  array (
    0 => 'language.types.type-juggling.php',
    1 => 'Jonglage de type',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    '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">Déclarations de type</h2>

 <p class="para">
  Les déclarations de types peuvent être ajoutées aux arguments des fonctions,
  valeurs de retour, et à partir de PHP 7.4.0, les propriétés de classe,
  et à partir de PHP 8.3.0, les constantes de classe.
  Elles assurent que la valeur est du type spécifié au moment de l&#039;appel,
  sinon une <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> est lancée.
 </p>

 <p class="para">
  Chaque type pris en charge par PHP, à l&#039;exception du type
  <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>, peut être utilisé dans une déclaration de type
  par l&#039;utilisateur.
  Cette page contient un journal des modifications de la disponibilité des
  différents types et de la documentation sur leur utilisation dans les
  déclarations de type.
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   Lorsqu&#039;une classe implémente une méthode d&#039;interface ou réimplémente une méthode
   qui a déjà été définie par une classe parente, elle doit être compatible avec la
   définition susmentionnée.
   Une méthode est compatible si elle suit les règles de
   <a href="language.oop5.variance.php" class="link">variance</a>.
  </p>
 </p></blockquote>

 <div class="sect2">
  <h3 class="title">Historique</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.3.0</td>
      <td>
       Ajout du support pour les constantes typées de classe, interface, trait, et enum.
      </td>
     </tr>

     <tr>
      <td>8.2.0</td>
      <td>
       Ajout du support de type <abbr title="Disjunctive Normal Form">DNF</abbr> (Forme Normale Disjonctive).
      </td>
     </tr>

     <tr>
      <td>8.2.0</td>
      <td>
       Ajout du support du type <span class="type"><a href="language.types.singleton.php" class="type true">true</a></span>.
      </td>
     </tr>

     <tr>
      <td>8.2.0</td>
      <td>
       Les types <span class="type"><a href="language.types.null.php" class="type null">null</a></span> et <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span> peuvent désormais être utilisés de manière autonome.
      </td>
     </tr>

     <tr>
      <td>8.1.0</td>
      <td>
       La prise en charge des types d’intersection a été ajoutée.
      </td>
     </tr>

     <tr>
      <td>8.1.0</td>
      <td>
       Le renvoi par référence à partir d’une fonction <span class="type"><span class="type"><a href="language.types.void.php" class="type void">void</a></span></span> est désormais déconseillé.
      </td>
     </tr>

     <tr>
      <td>8.1.0</td>
      <td>
       La prise en charge du type de retour uniquement <span class="type"><a href="language.types.never.php" class="type never">never</a></span> a été ajoutée.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Ajout du support de <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span>
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       La prise en charge du type de retour uniquement <span class="type">static</span> a été ajoutée.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       La prise en charge des types union a été ajoutée.
      </td>
     </tr>

     <tr>
      <td>7.4.0</td>
      <td>
       Ajout du support du typage des propriétés de classe.
      </td>
     </tr>

     <tr>
      <td>7.2.0</td>
      <td>
       Ajout du support pour <span class="type"><a href="language.types.object.php" class="type object">object</a></span>.
      </td>
     </tr>

     <tr>
      <td>7.1.0</td>
      <td>
       Ajout du support pour <span class="type"><a href="language.types.iterable.php" class="type iterable">iterable</a></span>.
      </td>
     </tr>

     <tr>
      <td>7.1.0</td>
      <td>
       Ajout du support pour <span class="type"><span class="type"><a href="language.types.void.php" class="type void">void</a></span></span>.
      </td>
     </tr>

     <tr>
      <td>7.1.0</td>
      <td>
       La prise en charge des types nullable a été ajoutée.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>

 <div class="sect2" id="language.types.declarations.base">
  <h3 class="title">Notes d&#039;utilisation des types atomiques</h3>

  <p class="simpara">
   Les types atomiques ont un comportement direct avec quelques mises en garde
   mineures qui sont décrites dans cette section.
  </p>

  <div class="sect3" id="language.types.declarations.base.scalar">
   <h4 class="title">Types scalaires</h4>
   <div class="warning"><strong class="warning">Avertissement</strong>
    <p class="para">
     Les alias pour les types scalaires (<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>) ne sont pas supportés.
     À la place, ils sont traités comme des noms de classe ou d&#039;interface.
     Par exemple, utiliser <code class="literal">boolean</code> comme une déclaration de
     type nécessite que la valeur soit une <a href="language.operators.type.php" class="link"><code class="literal">instanceof</code></a> de la classe ou
     interface <code class="literal">boolean</code>, plutôt que de 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="annotation-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">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">Résultat de l&#039;exemple ci-dessus en PHP 8 :</p>
     <div class="example-contents screen">
<div class="annotation-interactive 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">Note</strong>: 
    <p class="para">
     Le retour par référence à partir d’une fonction <span class="type"><span class="type"><a href="language.types.void.php" class="type void">void</a></span></span> est obsolète à partir
     de PHP 8.1.0, car une telle fonction est contradictoire.
     Auparavant, elle émettait déjà les <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> suivants lorsqu’elle était appelée :
     <span class="computeroutput">Only variable references should be returned by reference</span>.

     <div class="informalexample">
      <div class="example-contents">
<div class="annotation-interactive 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">Types Callable</h4>
   <p class="para">
    Ce type ne peut pas être utilisé comme déclaration de type de propriété de
    classe.
   </p>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     Il n’est pas possible de spécifier la signature de la fonction.
    </span>
   </p></blockquote>
  </div>

  <div class="sect3" id="language.types.declarations.references">
   <h4 class="title">Déclarations de type sur les paramètres de référence</h4>

   <p class="simpara">
    Si un paramètre passé par référence à une déclaration de type, le type
    de la variable <em>n’est vérifié qu’à</em> l’entrée de
    la fonction, au début de l’appel, mais pas lorsque la fonction retourne.
    Cela signifie qu’une fonction peut modifier le type de la variable passée
    par référence.
   </p>
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Paramètre typé passé par référence</strong></p>
    <div class="example-contents">
<div class="annotation-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">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>Résultat de l&#039;exemple ci-dessus est similaire à :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive 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">Notes d’utilisation des types composites</h3>
  <p class="para">
   Les déclarations de type composite sont soumises à quelques restrictions et
   effectueront un contrôle de redondance au moment de la compilation pour
   éviter les bogues simples.
  </p>

  <div class="caution"><strong class="caution">Attention</strong>
   <p class="simpara">
    Avant PHP 8.2.0 et l’introduction des types <abbr title="Disjunctive Normal Form">DNF</abbr>, il
    n’était pas possible de combiner les intersections de type avec les
    unions de type.
   </p>
  </div>

  <div class="sect3" id="language.types.declarations.composite.union">
   <h4 class="title">Types d’unions</h4>
   <div class="warning"><strong class="warning">Avertissement</strong>
    <p class="simpara">
     Il n’est pas possible de combiner les deux types de singleton
     <code class="literal">false</code> et <code class="literal">true</code> ensemble dans une
     union de type.
     Utiliser plutôt <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>.
    </p>
   </div>

   <div class="caution"><strong class="caution">Attention</strong>
    <p class="simpara">
     Avant PHP 8.2.0, comme <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span> et <span class="type"><a href="language.types.null.php" class="type null">null</a></span> ne
     pouvaient pas être utilisés comme types autonomes, une union de type
     composée uniquement de ces types n’était pas autorisée. Cela comprend les
     types suivants : <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span>, <code class="literal">false|null</code>
     et <span class="type">?false</span>.
    </p>
   </div>

   <div class="sect4" id="language.types.declarations.nullable">
    <h5 class="title">Sucre syntaxique de type nullable</h5>

    <p class="para">
     Une déclaration de type de base unique peut être marquée comme valeur NULL
     en faisant précéder le type d’un point d’interrogation (<code class="literal">?</code>).
     Ainsi <code class="literal">?T</code> et <code class="literal">T|null</code> sont identiques.
    </p>

    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      Cette syntaxe est prise en charge à partir de PHP 7.1.0, et est antérieure
      à la prise en charge généralisée des unions de type.
     </span>
    </p></blockquote>

    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      Il est également possible d’obtenir des arguments nullable en faisant de
      <code class="literal">null</code> la valeur par défaut.
      Ceci n’est pas recommandé, car si la valeur par défaut est modifiée dans
      une classe enfant, une violation de compatibilité de type sera déclenchée
      car le type <span class="type"><a href="language.types.null.php" class="type null">null</a></span> devra être ajouté à la déclaration de type.
      Ce comportement est également déprécié à partir de PHP 8.4.
     </p>
     <div class="example" id="example-2">
      <p><strong>Exemple #2 Ancienne façon de rendre les arguments nullables</strong></p>
      <div class="example-contents">
<div class="annotation-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">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>L&#039;exemple ci-dessus va afficher :</p></div>
      <div class="example-contents screen">
<div class="annotation-interactive 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">Types dupliqués et redondants</h4>
   <p class="para">
    Pour détecter des bogues simples dans les déclarations de type composite,
    les types redondants qui peuvent être détectés sans effectuer de chargement
    de classe entraîneront une erreur de compilation. Cela comprend :

    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       Chaque type résolu par nom ne peut se produire qu’une seule fois.
       Les types tels que <code class="literal">int|string|INT</code> ou
       <code class="literal">Countable&amp;Traversable&amp;COUNTABLE</code>
       génèrent une erreur.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       L’utilisation de <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> ou <span class="type"><a href="language.types.never.php" class="type never">never</a></span> entraîne une erreur.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">Pour les types d’unions :</span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara">
         Si <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> est utilisé, <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span> ou
         <span class="type"><a href="language.types.singleton.php" class="type true">true</a></span> ne peut pas être utilisé en plus.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         Si <span class="type"><a href="language.types.object.php" class="type object">object</a></span> est utilisé, les types de classe ne peuvent
         pas être utilisés en plus.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         Si <span class="type"><a href="language.types.iterable.php" class="type iterable">iterable</a></span> est utilisé, <span class="type"><a href="language.types.array.php" class="type array">array</a></span>
         et <span class="classname"><a href="class.traversable.php" class="classname">Traversable</a></span> ne peuvent pas être utilisés
         en plus.
        </span>
       </li>
      </ul>
     </li>
     <li class="listitem">
      <span class="simpara">Pour les types d’intersections :</span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara">
         L’utilisation d’un type qui n’est pas un type de classe génère une
         erreur.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         L’utilisation de <span class="type">self</span>, <span class="type">parent</span> ou
         <span class="type">static</span> entraîne une erreur.
        </span>
       </li>
      </ul>
     </li>
     <li class="listitem">
      <span class="simpara">Pour les types <abbr title="Disjunctive Normal Form">DNF</abbr> :</span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara">
         Si un type plus générique est utilisé, le plus restrictif est redondant.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         Utilisation de deux types d’intersection identiques.
        </span>
       </li>
      </ul>
     </li>
    </ul>
   </p>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     Cela ne garantit pas que le type est « minimal », car cela nécessiterait
     de charger tous les types de classe utilisés.
    </span>
   </p></blockquote>

   <p class="para">
    Par exemple, si <code class="literal">A</code> et <code class="literal">B</code> sont des
    alias de classe, alors <code class="literal">A|B</code> reste une union de type
    légale, même s&#039;il est possible de réduire à <code class="literal">A</code> ou
    <code class="literal">B</code>.
    De même, si la classe <code class="code">B extends A {}</code>, alors
    <code class="literal">A|B</code> est également une union de type légale, même s&#039;il
    pourrait être réduit au type <code class="literal">A</code> uniquement.
    <div class="informalexample">
     <div class="example-contents">
<div class="annotation-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">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">// Non autorisé<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">// Non autorisé<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">// Non autorisé<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">// Non autorisé<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">// Non autorisé ("use" fait partie de la résolution de noms)<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">// Non autorisé ("use" fait partie de la résolution de noms)<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">// Autorisé (la redondance n'est connue qu'au moment de l'exécution)<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">// Autorisé (la redondance n'est connue qu'au moment de l'exécution)<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">Exemples</h3>
  <div class="example" id="example-3">
   <p><strong>Exemple #3 Déclaration de type de classe de base</strong></p>
   <div class="example-contents">
<div class="annotation-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">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">// Ceci n'étend pas 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>Résultat de l&#039;exemple ci-dessus en PHP 8 :</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive 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>Exemple #4 Déclaration de type d’interface de base</strong></p>
   <div class="example-contents">
<div class="annotation-interactive 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">// Ceci n'implémente pas 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>Résultat de l&#039;exemple ci-dessus en PHP 8 :</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive 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>Exemple #5 Déclaration de type de retour de base</strong></p>
   <div class="example-contents">
<div class="annotation-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">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">// À noter qu'un float sera retourné.<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>L&#039;exemple ci-dessus va afficher :</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
float(3)
</pre></div>
   </div>
  </div>

  <div class="example" id="example-6">
   <p><strong>Exemple #6 Renvoi d’un objet</strong></p>
   <div class="example-contents">
<div class="annotation-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">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>L&#039;exemple ci-dessus va afficher :</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
object(C)#1 (0) {
}
</pre></div>
   </div>
  </div>

  <div class="example" id="example-7">
   <p><strong>Exemple #7 Déclaration de type d’argument nullable</strong></p>
   <div class="example-contents">
    <div class="annotation-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">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>L&#039;exemple ci-dessus va afficher :</p></div>
   <div class="example-contents screen">
    <div class="annotation-interactive cdata"><pre>
object(C)#1 (0) {
}
NULL
</pre></div>
   </div>
  </div>

  <div class="example" id="example-8">
   <p><strong>Exemple #8 Déclaration de type de retour nullable</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>Exemple #9 Déclaration de type pour les propriétés de classe</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">Typage Strict</h3>

  <p class="para">
   Par défaut, PHP va convertir les valeurs d&#039;un mauvais type vers le type
   scalaire attendu tant que possible. Par exemple, une fonction, qui attend
   comme paramètre une <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, à laquelle est passée un
   <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> recevra une variable de type <span class="type"><a href="language.types.string.php" class="type string">string</a></span>.
  </p>

  <p class="para">
   Il est possible d&#039;activer le mode de typage strict fichier par fichier.
   Dans le mode strict, seule une variable correspondant exactement au
   type attendu dans la déclaration sera acceptée sinon une
   <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> sera lancée.
   La seule exception à cette règle est qu&#039;une valeur de type <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
   peut passer une déclaration de type <span class="type"><a href="language.types.float.php" class="type float">float</a></span>.
  </p>

  <div class="warning"><strong class="warning">Avertissement</strong>
   <p class="simpara">
    Les appels aux fonctions depuis des fonctions internes ne seront pas
    affectés par la déclaration <code class="literal">strict_types</code>.
   </p>
  </div>

  <p class="para">
   Pour activer le mode strict, l&#039;expression <a href="control-structures.declare.php" class="link"><code class="literal">declare</code></a> est utilisée avec la
   déclaration <code class="literal">strict_types</code> :
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Le typage strict s&#039;applique aux appels de fonction effectués depuis
    <em>l&#039;intérieur</em> d&#039;un fichier dont le typage strict est
    actif, et non aux fonctions déclarées dans ce fichier. Si un fichier dont
    le typage strict n&#039;est pas activé effectue un appel à une fonction qui a
    été définie dans un fichier dont le type strict est actif, la préférence de
    l&#039;appelant (mode coercitif) sera respectée et la valeur sera forcée.
   </p>
  </p></blockquote>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Le typage strict n&#039;est défini que pour les déclarations de type scalaire.
   </p>
  </p></blockquote>

  <div class="example" id="example-10">
   <p><strong>Exemple #10 Typage strict pour les valeurs d&#039;arguments</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>Résultat de l&#039;exemple ci-dessus en PHP 8 :</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive 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>Exemple #11 Typage coercitif pour les valeurs d&#039;arguments</strong></p>
   <div class="example-contents">
<div class="annotation-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">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">// Ceux-ci seront convertis en entiers : à noter la sortie ci-dessous !<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>L&#039;exemple ci-dessus va afficher :</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
int(3)
int(3)
</pre></div>
   </div>
  </div>

  <div class="example" id="example-12">
   <p><strong>Exemple #12 Typage strict pour les valeurs de retour</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>L&#039;exemple ci-dessus va afficher :</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive 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); ?>