PHP Reflection - Get Method Parameter Type As String

后端 未结 6 1875
無奈伤痛
無奈伤痛 2020-12-01 18:44

I\'m trying to use PHP reflection to dynamically load the class files of models automatically based upon the type of parameter that is in the controller method. Here\'s an e

6条回答
  •  心在旅途
    2020-12-01 19:32

    I think the only way is to export and manipulate the result string:

    $refParam = new ReflectionParameter(array('Foo', 'Bar'), 0);
    
    $export = ReflectionParameter::export(
       array(
          $refParam->getDeclaringClass()->name, 
          $refParam->getDeclaringFunction()->name
       ), 
       $refParam->name, 
       true
    );
    
    $type = preg_replace('/.*?(\w+)\s+\$'.$refParam->name.'.*/', '\\1', $export);
    echo $type;
    

提交回复
热议问题