将 PHP 匿名函数转换为非匿名函数

发布于 09-24 11:43 字数 1508 浏览 3 评论 0原文

在丢失登录 ID 之前,我问了一个相关问题 - PHP 版本 5.2.14 / 解析错误:语法错误,意外的 T_FUNCTION,期待 ')' - 但这是“整个”问题。

我很难弄清楚如何转换这个函数(从 SO 上的某个地方获得)以与 PHP 5.2.14 一起使用(正如人们告诉我的那样 - 不支持匿名函数)。我尝试了一些更改代码的排列,以使其与 array_map() 一起使用,但我无法理解一切是如何工作的!

整个函数粘贴在下面,但只有指出的区域是 PHP 5.2.14 抱怨的区域。

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一江春梦2024-10-01 11:43:10

array_map 只是对 $convertedAvail 的每个元素执行匿名函数。您可以使用 foreach 循环遍历元素并调用 setTimezone(),而不是 array_map

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

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);
    }
}
青朷2024-10-01 11:43:10

嗯,正如前面提到的 PHP < 5.3 不支持匿名函数,因此你必须定义一些全局函数。

常见解决方案:

# PHP 5.3+

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

# PHP < 5.3

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

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

将上述代码应用到您的代码中,现在应该很容易了。

使用 create_function()

PHP <5.3 支持某种匿名函数 - 您可以使用 create_function()。尽管它不会乱七八糟地破坏您的全局名称空间,但它非常不可读且难以维护,因此我不建议使用该解决方案。

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.

若水微香2024-10-01 11:43:10

您可以在外部定期使用名称定义该函数,并将其名称提供给 array_map(),而不是在 array_map() 调用中使用内联函数。您只需在该函数内 global $tz2 即可访问该值。

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);

基于您的代码的重构建议假设它有效,但没有测试。

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.

南渊2024-10-01 11:43:09

如果您同意修改 $coneredAvail ,那么您可以使用 array_walk 并传递用户定义的数据:

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

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

我还没有测试过。但如果您之前的代码可以在 5.3 上运行,那么这个代码应该可以在 5.2 上运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文