2012-07-15 41 views
1

我測試PHP的默認PARAMS這樣的反思...提取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__)); 

    } 

} 

的問題是該行的「新ReflectionParameter(陣列($類,$法),$ param->名);」當執行「$ data [$ param-> name] =($ paramDetail-> isDefaultValueAvailable)」 表示沒有isDefaultValueAvailable也不是isOptional。

任何想法如何從類方法提取可選參數? 它似乎與功能正常工作。

回答

3

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" 
} 
+0

是的,我知道它......正如我在我的代碼中看到的唯一的問題是,我忘了把它當作一種方法來對待。 +1爲正確的答案。 – dyoser 2012-07-15 16:59:02

+0

'新的ReflectionParameter(...)'不需要,因爲'$ param'已經是'ReflectionParameter'的一個實例 – 2014-11-13 14:24:40

+0

@ThanhTrung,謝謝我更新了答案 – complex857 2014-11-13 14:59:02

0

isOptional和isDefaultValueAvailable有兩種方法而不是屬性。