TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Arr::range ? Why this way?
  • I have recently came across Arr::range function. I've never used it, but it hits me into the eyes how it is designed in Ko3.1 This is the source

     public static function range($step = 10, $max = 100)
     {
        if ($step < 1)
           return array();
    
       $array = array();
       for ($i = $step; $i <= $max; $i += $step)
       {
           $array[$i] = $i;
       }
    
        return $array;
    }
    

    Why it is this way? It is usable only in several special cases and also it is incompatible with PHP range function. What if i'd like range from 0 to 100 by step of 10? I'd expect

     public static function range($min = 0, $max = 100, $step=1)
     {
        if ($step < 1 OR $min > $max)
            return array();
    
        $array = array();
        for ($i = $min; $i <= $max; $i += $step)
        {
            $array[$i] = $i;
        }
    
        return $array;
    }
    
  • Why change the function so that it just reproduces the PHP range() function?

  • It does not reproduce just php range function. This function returns val=>val array. Purpose of this function is mainly to use it in Form::select() I think and to avoid such constructions as array_combine(range($min,$max),range($min,$max)). The main disadvantage of original function for me is, that I can not specify different start point than the step. What if I want in for ex. e-shop to sell product by step of ten, but minimum is 50 pieces? In my point of view the first function is nearly useless.

  • I think there's an issue for 3.3 to change this.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion