如何在PHP 5.2版本上使用像function()use()这样的PHP闭包函数? [英] How can I use PHP closure function like function() use() on PHP 5.2 version?

查看:113
本文介绍了如何在PHP 5.2版本上使用像function()use()这样的PHP闭包函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在PHP 5.2版本上使用像function()use()这样的PHP闭包函数,因为它不支持匿名函数?

How can I use PHP closure function like function() use() on PHP 5.2 version as it has no support for anonymous functions?

当前我的代码如下所示

$this->init(function() use($taxonomy_name, $plural, $post_type_name, $options)
{
   // Override defaults with user provided options

    $options = array_merge(
       array(
          "hierarchical" => false,
          "label" => $taxonomy_name,
          "singular_label" => $plural,
          "show_ui" => true,
          "query_var" => true,
          "rewrite" => array("slug" => strtolower($taxonomy_name))
        ), $options
      );

      // name of taxonomy, associated post type, options
      register_taxonomy(strtolower($taxonomy_name), $post_type_name, $options);
});

推荐答案

Php从5.3.0开始支持匿名功能,请在

Php supports anonymous functions since 5.3.0, read about it in manual.

您可以使用create_function,但是您应该避免使用此命令(例如,由于

You could use create_function but you should avoid this (for example because of this comment) and it's practically the same as eval... One bad enclosure and you'll make your sources vulnerable. It's also evaluted on runtime, not on compilation time what can decrease performance and may cause fatal error in the middle of included file.

宁可在某个地方声明该函数,它也会更有效.

Rather declare that function somewhere, it'll be more effective.

这篇关于如何在PHP 5.2版本上使用像function()use()这样的PHP闭包函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆