このようなデフォルトのパラメータでphpのリフレクションをテストしています...
class Test{
public function testMethod($class,$methodName){
// the returned values
$data = array();
// get the method of the class passed by params
$funcHandler = new ReflectionMethod($class,$methodName);
// iterates through the parameters to catch de default values
foreach ($funcHandler->getParameters() as $param){
// instance the params to get the properties for that method
$paramDetail = new ReflectionParameter(array($class, $method),$param->name);
// check if that param is or has a default value attached like (public function method($a,$b,$c = false, $d = null)
$data[$param->name] = ($paramDetail->isDefaultValueAvailable) ? funcHandler->getDefaultValue : '';
return $data;
}
}
//let's test the reflection with the method params...
class Foo{
public function method1($a,$b,$c = false, $d = null, $e = 'hello'){
// instance of the Test Class
$obj = new Test();
// calling the test method with this class and method names
print_r($obj->testMethod(__CLASS__,__FUNCTION__));
}
}
問題は、「new ReflectionParameter(array($class, $method),$param->name);」という行です。"$data[$param->name] = ($paramDetail->isDefaultValueAvailable) ? " を実行すると、 isDefaultValueAvailable も isOptional もありません。
クラスメソッドのオプションのパラメータから抽出する方法はありますか? 機能的には問題ないようです。