PHP multidimensional array type hinting for function parameter -


in php, possible type hint multidimensional array function parameter?

of course can type hint single array such:

function example(array $parameter) {} 

but if function needs parameter multidimensional array there way enforce through type hinting?

if doublearray meant array of doubles (floats) only. may want fake using custom class. consider code below. notice initial array contains strings final results doesn't:

<?php      class doublearray{         protected $instance;         public function __construct(array $arrdoubles) {             foreach($arrdoubles $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 not double");     $da     = new doublearray($arr);      var_dump(getdata($da));       function getdata(doublearray $data){         // array functions still apply $doublearray variable         // custom double array data, may have         // call get() method on doublearray instance: $data         $doublearray    = $data->get();         return $doublearray;     }      // 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 
however, if doublearray meant multidimensional arrays, can still fake using custom class illustrated below.
<?php      class doublearray{         protected $instance;          public function __construct(array $arrmddoubles) {             foreach($arrmddoubles $key=>$subarray) {                 if(!is_array($subarray)){                     throw new exception("doublearray accepts multidimensional arrays...");                 }             }             $this->instance = $arrmddoubles;         }          public function push($arrarray, $key=null){             if(!is_array($arrarray)){                 throw new exception("you can push 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 not double"));     $da     = new doublearray($arr);      var_dump(getdata($da));       function getdata(doublearray $data){         // array functions still apply $doublearray variable         // custom double array data, may have         // call get() method on doublearray instance: $data         $doublearray    = $data->get();         return $doublearray;     }      // time the var_dump(getdata($da) above throw exception     // if given array not multi-dimensional produce     // 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 not double' (length=20) 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -