<?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 => 'ja',
  ),
  'this' => 
  array (
    0 => 'reflectionparameter.gettype.php',
    1 => 'ReflectionParameter::getType',
    2 => '引数の型を取得する',
  ),
  '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' => 'ja',
    '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">引数の型を取得する</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-reflectionparameter.gettype-description">
  <h3 class="title">説明</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">
   引数の型を取得します。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-reflectionparameter.gettype-parameters">
  <h3 class="title">パラメータ</h3>
  <p class="para">この関数にはパラメータはありません。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-reflectionparameter.gettype-returnvalues">
  <h3 class="title">戻り値</h3>
  <p class="para">
   引数の型が指定されていれば <span class="classname"><a href="class.reflectiontype.php" class="classname">ReflectionType</a></span> オブジェクトを返します。
   そうでなければ <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> を返します。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-reflectionparameter.gettype-examples">
  <h3 class="title">例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>例1 PHP 7.1.0 以降での <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> の使い方</strong></p>
    <div class="example-contents"><p>
     PHP 7.1.0 以降では、
     <span class="methodname"><a href="reflectiontype.tostring.php" class="methodname">ReflectionType::__toString()</a></span> が推奨されなくなったので、
     <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> は
     <span class="classname"><a href="class.reflectionnamedtype.php" class="classname">ReflectionNamedType</a></span> を返す <em>可能性があります</em>。
     この場合、引数の方の名前を取得するために、<span class="methodname"><strong>ReflectionNamedType()</strong></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">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>上の例の出力は以下となります。</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>例2 PHP 7.1.0 より前の <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> の使い方</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>上の例の 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>例3 PHP 8.0.0 以降の <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> の使い方</strong></p>
    <div class="example-contents"><p>
     PHP 8.0.0 以降では、このメソッドは
     <span class="classname"><a href="class.reflectionnamedtype.php" class="classname">ReflectionNamedType</a></span> や
     <span class="classname"><a href="class.reflectionuniontype.php" class="classname">ReflectionUnionType</a></span> のインスタンスを返す可能性があります。
     後者は前者の集合です。後者の型を解析するには、
     <span class="classname"><a href="class.reflectionnamedtype.php" class="classname">ReflectionNamedType</a></span> の配列に正規化する方が便利です。
     以下の例に示す関数は、<code class="literal">0</code> または
     <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 />    if (!</span><span style="color: #0000BB">$reflectionType</span><span style="color: #007700">) return [];<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">参考</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="reflectionparameter.hastype.php" class="methodname" rel="rdfs-seeAlso">ReflectionParameter::hasType()</a> - 引数が型を持つかを調べる</span></li>
    <li><span class="methodname"><a href="reflectiontype.tostring.php" class="methodname" rel="rdfs-seeAlso">ReflectionType::__toString()</a> - 文字列に変換する</span></li>
   </ul>
  </p>
 </div>


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