Making a PHP closure function safe for PHP 5.2

蓝咒 提交于 2019-12-10 19:08:31

问题


The following function works in PHP > 5.3 but errors out in older versions. How can I modify this to make it 5.2 safe?

function _iniloader_get_dirs($dir) {
        $dirs = array_filter(scandir($dir), function ($item) use ($dir) {
            return (is_dir($dir.'/'.$item) && $item != "." && $item != "..");
        });
        // Use array_values to reset the array keys:
        return array_values($dirs);
}

5.2 error:

Parse error: syntax error, unexpected T_FUNCTION ... on line 2


回答1:


You can easily do it w/o the closure, but you will lose some elegance:

function _iniloader_get_dirs($dir) {
    $dirs = array();
    $entries = scandir($dir);
    foreach($entries as $item) {
        if($item === '.' || $item === '..') continue;
        if(!is_dir($dir.'/'.$item)) continue;
        $dirs[] = $item;
    }
    return $dirs;
}



回答2:


Normally the way to mimic closures in PHP<5.3 is with create_function() but because of the use clause in this case, I can't come up with a way to do it without doing something horrible involving bouncing variables through the global scope. There may be something I'm missing.

In any case, here is some code that will do the same thing without requiring a closure:

function _iniloader_get_dirs($dir) {
    $dirs = array();
    foreach (scandir($dir) as $item) {
        if (is_dir($dir.'/'.$item) && !in_array($item, array('.', '..'))) {
            $dirs[] = $item;
        }
    }
    return $dirs;
}



回答3:


You can do it like this (untested). The idea is to insert the value of $dir into the source of the function.

function _iniloader_get_dirs($dir) {
    $dirs = array_filter(scandir($dir),
        create_function('$item', '$dir = \'' . $dir . '\';' .
            'return (is_dir($dir."/".$item) && $item != "." && $item != "..");'));
    return array_values($dirs);
}

Beware if you call this function a lot, because every time create_function is called, it creates a new function in the program memory that stays forever, even if it is essentially the same as other functions except a variable. So if you call this a lot then the program will be filled with dummy functions and run out of memory.



来源:https://stackoverflow.com/questions/8897496/making-a-php-closure-function-safe-for-php-5-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!