开发者

Converting a PHP anonymous function to non-anonymous

I asked a related question before I lost my login id - PHP Version 5.2.14 / Parse error: syntax error, unexpected T_FUNCTION, expecting ')' - but this is the "entire" problem.

I'm having a hard time figuring out how to convert this function (got from somewhere on SO) to work with PHP 5.2.14 (which as folks informed me - does not support anonymous functions). I tried a few permutations of changing the code to make it work with array_map() but I cant wrap my head around how everything works!

The entire function is pasted below, but only the areas pointed out are the ones that PHP 5.2.14 complains about..

function convertGeneralAvailabilityTime($date,$from_timezone,$from_timebegin, $from_timeend, $to_timezone)
{

$tz1 = new DateTimezone($from_timezone);

$datetime1 = new DateTime("$date $from_timebegin", $tz1);
$datetime2 = new DateTime("$date $from_timeend", $tz1);

$convertedAvail = array(
    array($datetime1, $datetime2),
);


$tz2 = new DateTimezone($to_timezone);
//convert periods:

// ISSUE_START

$times = array_map(
    function (array $p) use ($tz2) {
       $res = array();
       foreach ($p as $d) {
           $res[] = $d->setTimezone($tz2);
       }
       return $res;
    },
    $convertedAvail
);

// ISSUE_END

$res = array();
foreach ($times as $t) {
    $t1 = reset($t);
    $t2 = next($t);
    if ($t1->format("d") == $t2->format("d")) {
        $res[$t1->format("l")][] = $t1->format("g:i a") . " to ".
            $t2->format("g:i a");
    }
    else {
        $res[$t1->format("l")][] = $t1->format("g:i a") . " to 11:59 pm";
        $res[$t2->format("l")][] = "12:00 am to ". $t2->format("g:i a");
    }
}开发者_运维问答

return $res;
}


If you are okay with $converedAvail to be modified, then you could use array_walk and pass user defined data:

function convertTimezone(array &$p, $key, $tz2) {
    foreach ($p as &$d) {
        $d = $d->setTimezone($tz2);
    }
}

array_walk($convertedAvail, 'convertTimezone', $tz2);

I haven't tested it. But if your previous code worked on 5.3, then this one should work on 5.2.


array_map simply executes the anonymous function for every element of $convertedAvail. Instead of array_map, you can loop over the elements using foreach and call setTimezone()

foreach ($convertedAvail as $cKey => $dateArray)
{
    foreach ($dateArray as $dKey => $date)
    {
        $convertedAvail[$cKey][$dKey]->setTimezone($tz2);
    }
}


Well, as mentioned PHP < 5.3 does not support anonymous functions, therefore you have to define some global function.

Common solution:

# PHP 5.3+

$array = array_map(function() {
    return /* do sth */;
}, $array);

# PHP < 5.3

function abcMyFunction() {
    return /* do sth */;
}

$array = array_map('abcMyFunction', $array);

Applying the above code to your, should be easy now.

Using create_function():

PHP <5.3 supports some kind of anonymous functions - you can define them using create_function(). Although it doesn't litter your global namespace it's so unreadable and hard to maintain that I wouldn't suggest to use that solution.


Instead of using the function inline in the array_map() call, you can define it outside regularly with a name, and feed its name to array_map(). You can just global $tz2 inside that function to have access to that value.

function set_timezone_callback($p) {
    global $tz2;
    $res = array();
    foreach ($p as $d) {
        $res[] = $d->setTimezone($tz2);
    }
    return $res;
}

$times = array_map('set_timezone_callback', $convertedAvail);

refactoring suggestion based on your code assuming it works, didn't test.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜