【问题标题】:Extract PHP class method REFLECTION values提取 PHP 类方法 REFLECTION 值
【发布时间】:2012-07-15 14:51:50
【问题描述】:

我正在使用这样的默认参数测试 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。

任何想法如何从类方法中提取可选参数? 它似乎与函数配合得很好。

【问题讨论】:

    标签: php reflection


    【解决方案1】:

    ReflectionParameter 类有一个isOptional() 方法,它会告诉你参数是否可选(只有当它有默认值时才可以是可选的),如果是可选的,你可以调用getDefaultValue() 提取默认值。

    这里是你的代码修补使用它们:

    <?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){
                // the new ReflectionParameter ... not needed, getParameters() already give you ReflectionParameter instances
                // check if that param (a ReflectionParameter instance) has a default value attached like (public function method($a,$b,$c = false, $d = null)
                $data[$param->name] = ($param->isOptional()) ? $param->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
            var_dump($obj->testMethod(__CLASS__,__FUNCTION__));
    
        }
    
    }
    
    $f = new Foo;
    $f->method1('a', 'b');
    

    输出:

    array(5) {
      ["a"]=>
      string(0) ""
      ["b"]=>
      string(0) ""
      ["c"]=>
      bool(false)
      ["d"]=>
      NULL
      ["e"]=>
      string(5) "hello"
    }
    

    【讨论】:

    • 是的,我知道...正如我在代码中看到的,唯一的问题是我忘记将其视为一种方法 LOL :D。为正确答案 +1。
    • new ReflectionParameter(...) 不需要,因为$param 已经是ReflectionParameter 的实例
    【解决方案2】:

    isOptional 和 isDefaultValueAvailable 都是方法而不是属性。

    【讨论】:

      猜你喜欢
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多