PHP Conference Japan 2024

ReflectionParameter::__construct

(PHP 5, PHP 7, PHP 8)

ReflectionParameter::__construct建構子

說明

public ReflectionParameter::__construct(字串|陣列|物件 $function, 整數|字串 $param)

建構一個 ReflectionParameter 實例。

參數

function

要反射參數的函式。

param

指定參數位置的 整數(從零開始),或是參數名稱的 字串

範例

範例 #1 使用 ReflectionParameter 類別

<?php
function foo($a, $b, $c) { }
function
bar(Exception $a, &$b, $c) { }
function
baz(ReflectionFunction $a, $b = 1, $c = null) { }
function
abc() { }

$reflect = new ReflectionFunction('foo');

echo
$reflect;

foreach (
$reflect->getParameters() as $i => $param) {
printf(
"-- 參數 #%d: %s {\n".
" 類別: %s\n".
" 允許 NULL: %s\n".
" 以參考傳遞: %s\n".
" 是否為選用?: %s\n".
"}\n",
$i, // $param->getPosition() 可以使用
$param->getName(),
var_export($param->getClass(), 1),
var_export($param->allowsNull(), 1),
var_export($param->isPassedByReference(), 1),
$param->isOptional() ? 'yes' : 'no'
);
}
?>

上述範例將輸出類似以下的內容

Function [ <user> function foo ] {
  @@ /Users/philip/cvs/phpdoc/a 2 - 2

  - Parameters [3] {
    Parameter #0 [ <required> $a ]
    Parameter #1 [ <required> $b ]
    Parameter #2 [ <required> $c ]
  }
}
-- Parameter #0: a {
   Class: NULL
   Allows NULL: true
   Passed to by reference: false
   Is optional?: no
}
-- Parameter #1: b {
   Class: NULL
   Allows NULL: true
   Passed to by reference: false
   Is optional?: no
}
-- Parameter #2: c {
   Class: NULL
   Allows NULL: true
   Passed to by reference: false
   Is optional?: no
}

參見

新增註釋

使用者貢獻的筆記 1 則筆記

tracid2008 t gmail o com
12 年前
您也可以使用類別而不是函式名稱。只需像這樣使用陣列
<?php
$reflect
= new ReflectionParameter(array('className', 'methodName'), 'property');
?>
To Top