<?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 => 'es',
  ),
  'this' => 
  array (
    0 => 'reflectionparameter.gettype.php',
    1 => 'ReflectionParameter::getType',
    2 => 'Obtiene el tipo del par&aacute;metro',
  ),
  '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' => 'es',
    '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">Obtiene el tipo del parámetro</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-reflectionparameter.gettype-description">
  <h3 class="title">Descripción</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">
   Obtiene el tipo asociado de un parámetro.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-reflectionparameter.gettype-parameters">
  <h3 class="title">Parámetros</h3>
  <p class="para">Esta función no contiene ningún parámetro.</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-reflectionparameter.gettype-returnvalues">
  <h3 class="title">Valores devueltos</h3>
  <p class="para">
   Devuelve un objeto <span class="classname"><a href="class.reflectiontype.php" class="classname">ReflectionType</a></span> si se especifica un tipo de
   parámetro, <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> en caso contrario.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-reflectionparameter.gettype-examples">
  <h3 class="title">Ejemplos</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Ejemplo #1 Uso de <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> a partir de PHP 7.1.0</strong></p>
    <div class="example-contents"><p>
     A partir de PHP 7.1.0, <span class="methodname"><a href="reflectiontype.tostring.php" class="methodname">ReflectionType::__toString()</a></span> está obsoleto, y
     <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> <em>puede</em> devolver una
     instancia de <span class="classname"><a href="class.reflectionnamedtype.php" class="classname">ReflectionNamedType</a></span>. Para obtener el nombre del tipo de parámetro,
     <span class="methodname"><strong>ReflectionNamedType()</strong></span> está disponible en este caso.
    </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>El ejemplo anterior mostrará:</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>Ejemplo #2 Uso de <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> anterior a 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>Resultado del ejemplo anterior 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>Ejemplo #3 Uso de <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> en PHP 8.0.0 y posteriores</strong></p>
    <div class="example-contents"><p>
     A partir de PHP 8.0.0, este método puede devolver una instancia de
     <span class="classname"><a href="class.reflectionnamedtype.php" class="classname">ReflectionNamedType</a></span> o de <span class="classname"><a href="class.reflectionuniontype.php" class="classname">ReflectionUnionType</a></span>.
     Lo siguiente es una colección del primero. Para analizar un tipo,
     es a menudo práctico normalizarlo en un array de objetos
     <span class="classname"><a href="class.reflectionnamedtype.php" class="classname">ReflectionNamedType</a></span>. La función siguiente devolverá
     un array de <code class="literal">0</code> o más instancias 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">Ver también</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="reflectionparameter.hastype.php" class="methodname" rel="rdfs-seeAlso">ReflectionParameter::hasType()</a> - Verifica si un par&aacute;metro tiene un tipo</span></li>
    <li><span class="methodname"><a href="reflectiontype.tostring.php" class="methodname" rel="rdfs-seeAlso">ReflectionType::__toString()</a> - Conversi&oacute;n a string</span></li>
   </ul>
  </p>
 </div>


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