2016-07-25 54 views
0

在PHP中,是否可以鍵入提示多維數組作爲函數參數?PHP多維數組類型提示功能參數

當然,你可以鍵入提示單個陣列這樣:

function Example(array $parameter) {} 

但是,如果一個函數需要的參數是一個多維數組有沒有辦法通過類型提示強制執行呢?

+0

PHP不像Java或C#那樣強類型化。這就是爲什麼你甚至有像PHP中的'mixed'這樣的概念。所以,PHP不關心你的數組是否只能包含雙精度或字符串或「混合」,但如果你需要這個功能,你可以自己實現一個檢查*。僞造它的一種方法是創建一個類:'DoubleArray',然後將其用於Type-Hinting ... – Poiz

+2

並且double是指多維或者值是雙精度(浮點數)? – AbraCadaver

+0

@AbraCadaver多維數組,對不起,我會更新問題以便清除 –

回答

1

如果按DoubleArray您的意思只是一個雙數組(浮點數)。然後你可能想用自定義類來僞造它。考慮下面的代碼。請注意,最初的Array包含strings但最終結果不會:

<?php 

    class DoubleArray{ 
     protected $instance; 
     public function __construct(array $arrDoubles) { 
      foreach($arrDoubles as $key=>$double) { 
       if(!is_double($double)){ 
        unset($arrDoubles[$key]); 
       } 
      } 
      $this->instance = array_values($arrDoubles); 
     } 

     public function push($numDouble){ 
      $this->instance[] = $numDouble; 
     } 

     public function get(){ 
      return $this->instance; 
     } 
    } 

    $arr = array(2.35, 72.9, 88.45, 42.76, "No...", 57.77, "String is not Double"); 
    $da  = new DoubleArray($arr); 

    var_dump(getData($da)); 


    function getData(DoubleArray $data){ 
     // ALL ARRAY FUNCTIONS STILL APPLY TO THE $doubleArray VARIABLE 
     // HOWEVER TO GET THE CUSTOM DOUBLE ARRAY DATA, YOU MAY HAVE TO 
     // JUST CALL THE get() METHOD ON THE DoubleArray INSTANCE: $data 
     $doubleArray = $data->get(); 
     return $doubleArray; 
    } 

    // THE var_dump(getData($da); ABOVE PRODUCES: 
    array (size=5) 
     0 => float 2.35 
     1 => float 72.93 
     2 => float 88.45 
     3 => float 42.76 
     4 => float 57.77 
然而,如果 DoubleArray你的意思 Multidimensional Arrays,你仍然可以使用它自定義類,如下圖所示假貨。
<?php 

    class DoubleArray{ 
     protected $instance; 

     public function __construct(array $arrMDDoubles) { 
      foreach($arrMDDoubles as $key=>$subArray) { 
       if(!is_array($subArray)){ 
        throw new Exception("DoubleArray accepts only Multidimensional Arrays..."); 
       } 
      } 
      $this->instance = $arrMDDoubles; 
     } 

     public function push($arrArray, $key=null){ 
      if(!is_array($arrArray)){ 
       throw new Exception("You can only Push an Array..."); 
      } 
      if(!is_null($key)){ 
       $this->instance[$key] = $arrArray; 
      }else{ 
       $this->instance[]  = $arrArray; 
      } 
      return $this; 
     } 

     public function get(){ 
      return $this->instance; 
     } 
    } 

    $arr = array(array(2.35, 72.93,), array(88.45, 42.76), array("No...", 57.77, "String is not Double")); 
    $da  = new DoubleArray($arr); 

    var_dump(getData($da)); 


    function getData(DoubleArray $data){ 
     // ALL ARRAY FUNCTIONS STILL APPLY TO THE $doubleArray VARIABLE 
     // HOWEVER TO GET THE CUSTOM DOUBLE ARRAY DATA, YOU MAY HAVE TO 
     // JUST CALL THE get() METHOD ON THE DoubleArray INSTANCE: $data 
     $doubleArray = $data->get(); 
     return $doubleArray; 
    } 

    // THIS TIME THE THE var_dump(getData($da) ABOVE WOULD THROW AN EXCEPTION 
    // IF THE GIVEN ARRAY IS NOT MULTI-DIMENSIONAL BUT PRODUCE SOMETHING 
    // LIKE THIS OTHERWISE:  
    array (size=3) 
     0 => 
     array (size=2) 
      0 => float 2.35 
      1 => float 72.93 
     1 => 
     array (size=2) 
      0 => float 88.45 
      1 => float 42.76 
     2 => 
     array (size=3) 
      0 => string 'No...' (length=5) 
      1 => float 57.77 
      2 => string 'String is not Double' (length=20) 
+0

錯誤地說我的意思是多維數組,但我沒有想過使用類來強制參數類型,謝謝! –

+1

'multidimensional-array'標籤從一開始就在那裏。 – revo

+0

@revo然而,即使是像AbraCadaver這樣的大師也問:*雙倍你的意思是多維度還是值是雙打(浮點數)?*你也會讓他知道嗎? – Poiz