简体   繁体   中英

PHP multidimensional array type hinting for function parameter

In PHP, is it possible to type hint a multidimensional array as a function parameter?

Of course you can type hint a single array as such:

function Example(array $parameter) {}

But if a function needs the parameter to be a multidimensional array is there any way to enforce this through type hinting?

If by DoubleArray you meant an Array of Doubles (Floats) only. Then you may want to fake it using a Custom Class. Consider the Code below. Notice that the initial Array Contains strings but the final results doesn't:

<?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
However, if by DoubleArray you meant Multidimensional Arrays , you can still fake it using a Custom Class as illustrated below.
<?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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM