<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.reflectionparameter.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'reflectionparameter.gettype.php',
    1 => 'ReflectionParameter::getType',
    2 => 'R&eacute;cup&egrave;re le type du param&egrave;tre',
  ),
  'up' => 
  array (
    0 => 'class.reflectionparameter.php',
    1 => 'ReflectionParameter',
  ),
  'prev' => 
  array (
    0 => 'reflectionparameter.getposition.php',
    1 => 'ReflectionParameter::getPosition',
  ),
  'next' => 
  array (
    0 => 'reflectionparameter.hastype.php',
    1 => 'ReflectionParameter::hasType',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/reflection/reflectionparameter/gettype.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="reflectionparameter.gettype" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ReflectionParameter::getType</h1>
  <p class="verinfo">(PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">ReflectionParameter::getType</span> &mdash; <span class="dc-title">Récupère le type du paramètre</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-reflectionparameter.gettype-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>ReflectionParameter::getType</strong></span>(): <span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="class.reflectiontype.php" class="type ReflectionType">ReflectionType</a></span></span></div>

  <p class="para rdfs-comment">
   Récupère le type associé d&#039;un paramètre.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-reflectionparameter.gettype-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">Cette fonction ne contient aucun paramètre.</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-reflectionparameter.gettype-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Retourne un objet <span class="classname"><a href="class.reflectiontype.php" class="classname">ReflectionType</a></span> si un type de
   paramètre est spécifié, <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> sinon.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-reflectionparameter.gettype-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Utilisation de <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> à partir de PHP 7.1.0</strong></p>
    <div class="example-contents"><p>
     À partir de PHP 7.1.0, <span class="methodname"><a href="reflectiontype.tostring.php" class="methodname">ReflectionType::__toString()</a></span> est obsolète, et
     <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> <em>peut</em> retourner une
     instance de <span class="classname"><a href="class.reflectionnamedtype.php" class="classname">ReflectionNamedType</a></span>. Pour récupérer le nom du type de paramètre,
     <span class="methodname"><strong>ReflectionNamedType()</strong></span> est disponible dans ce cas.
    </p></div>
    <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">someFunction</span><span style="color: #007700">(</span><span style="color: #0000BB">int $param</span><span style="color: #007700">, </span><span style="color: #0000BB">$param2</span><span style="color: #007700">) {}<br /><br /></span><span style="color: #0000BB">$reflectionFunc </span><span style="color: #007700">= new </span><span style="color: #0000BB">ReflectionFunction</span><span style="color: #007700">(</span><span style="color: #DD0000">'someFunction'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$reflectionParams </span><span style="color: #007700">= </span><span style="color: #0000BB">$reflectionFunc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getParameters</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$reflectionType1 </span><span style="color: #007700">= </span><span style="color: #0000BB">$reflectionParams</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getType</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$reflectionType2 </span><span style="color: #007700">= </span><span style="color: #0000BB">$reflectionParams</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getType</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">$reflectionType1 </span><span style="color: #007700">instanceof </span><span style="color: #0000BB">ReflectionNamedType</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$reflectionType1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$reflectionType2</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 examplescode"><pre class="examplescode">int
NULL</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Exemple #2 Utilisation de <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> antérieur à PHP 7.1.0</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">someFunction</span><span style="color: #007700">(</span><span style="color: #0000BB">int $param</span><span style="color: #007700">, </span><span style="color: #0000BB">$param2</span><span style="color: #007700">) {}<br /><br /></span><span style="color: #0000BB">$reflectionFunc </span><span style="color: #007700">= new </span><span style="color: #0000BB">ReflectionFunction</span><span style="color: #007700">(</span><span style="color: #DD0000">'someFunction'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$reflectionParams </span><span style="color: #007700">= </span><span style="color: #0000BB">$reflectionFunc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getParameters</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$reflectionType1 </span><span style="color: #007700">= </span><span style="color: #0000BB">$reflectionParams</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getType</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$reflectionType2 </span><span style="color: #007700">= </span><span style="color: #0000BB">$reflectionParams</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getType</span><span style="color: #007700">();<br /><br />echo </span><span style="color: #0000BB">$reflectionType1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$reflectionType2</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 7.0 :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">int
NULL</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-3">
    <p><strong>Exemple #3 Utilisation de <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> en PHP 8.0.0 et ultérieur</strong></p>
    <div class="example-contents"><p>
     À partir de PHP 8.0.0, cette méthode peut retourner une instance soit de
     <span class="classname"><a href="class.reflectionnamedtype.php" class="classname">ReflectionNamedType</a></span> soit de <span class="classname"><a href="class.reflectionuniontype.php" class="classname">ReflectionUnionType</a></span>.
     Le suivant est une collection du premier. Pour analyser un type,
     il est souvent pratique de le normaliser en un tableau d&#039;objets
     <span class="classname"><a href="class.reflectionnamedtype.php" class="classname">ReflectionNamedType</a></span>. La fonction suivante retournera
     un tableau de <code class="literal">0</code> ou plus d&#039;instances de
     <span class="classname"><a href="class.reflectionnamedtype.php" class="classname">ReflectionNamedType</a></span>
    </p></div>
    <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">getAllTypes</span><span style="color: #007700">(</span><span style="color: #0000BB">ReflectionParameter $reflectionParameter</span><span style="color: #007700">): array<br />{<br />    </span><span style="color: #0000BB">$reflectionType </span><span style="color: #007700">= </span><span style="color: #0000BB">$reflectionParameter</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getType</span><span style="color: #007700">();<br /><br />    if (!</span><span style="color: #0000BB">$reflectionType</span><span style="color: #007700">) return [];<br /><br />    return </span><span style="color: #0000BB">$reflectionType </span><span style="color: #007700">instanceof </span><span style="color: #0000BB">ReflectionUnionType<br />        </span><span style="color: #007700">? </span><span style="color: #0000BB">$reflectionType</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getTypes</span><span style="color: #007700">()<br />        : [</span><span style="color: #0000BB">$reflectionType</span><span style="color: #007700">];<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

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


 <div class="refsect1 seealso" id="refsect1-reflectionparameter.gettype-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="reflectionparameter.hastype.php" class="methodname" rel="rdfs-seeAlso">ReflectionParameter::hasType()</a> - V&eacute;rifie si un param&egrave;tre a un type</span></li>
    <li><span class="methodname"><a href="reflectiontype.tostring.php" class="methodname" rel="rdfs-seeAlso">ReflectionType::__toString()</a> - Conversion en cha&icirc;ne de caract&egrave;res</span></li>
   </ul>
  </p>
 </div>


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