Replaced each() function as it is deprecated in PHP 7.2

#35 Open

Bitbucket cannot automatically merge this request.

The commits that make up this pull request have been removed.

Bitbucket cannot automatically merge this request due to conflicts.

Review the conflicts on the Overview tab. You can then either decline the request or merge it manually on your local system using the following commands:

hg update codeigniter-3.x
hg pull -r gigilibl/replaced-each-function-as-it-is-deprecat-1524927932033
hg merge gigilibl/replaced-each-function-as-it-is-deprecat-1524927932033
hg commit -m 'Merged in gigilibl/codeigniter-modular-extensions-hmvc/gigilibl/replaced-each-function-as-it-is-deprecat-1524927932033 (pull request #35)'
  1. Igor Ilic

Replaced each() function as it is deprecated in PHP 7.2

Comments (2)

  1. William Poetra Yoga

    If I may, here’s my patch – you can read or ignore/delete the long comments. I think if we delete the verbose comments, my version would be somewhat shorter and easier to read.

    The author actually wrote the original line of code as an elegant way to get two variables $module and $params from the function parameter. He even saved some memory by overwriting the function parameter $module – not that I agree with this kind of coding style.

    --- ci3-hmvc/application/third_party/MX/Modules.php 2015-03-31 18:31:02.000000000 +0700
    +++ ci3-hmvc/application/third_party/MX/Modules.php 2018-05-15 16:07:43.579725591 +0700
    @@ -80,7 +80,24 @@
        /** Load a module controller **/
        public static function load($module) 
    -       (is_array($module)) ? list($module, $params) = each($module) : $params = NULL;  
    +       // previously, it was like this, but then each() got deprecated in php7.2
    +       // (is_array($module)) ? list($module, $params) = each($module) : $params = NULL;   
    +       // analysis:
    +       // $module gets overwritten by the key returned by each()
    +       // also, $module was passed by value -- anything we do here does not affect the original array
    +       // so we don't need to emulate each() perfectly, rather we only need to get the key and value
    +       // at the current internal pointer position
    +       if (is_array($module))
    +       {
    +           // need to get the $params first, because the second call overwrites $module
    +           $params = current($module);
    +           $module = key($module);
    +       }
    +       else
    +       {
    +           $params = NULL;
    +       }
            /* get the requested controller class name */
            $alias = strtolower(basename($module));