如何在PHP5.2版本上使用类似function()use()的PHP闭包函数?
PHP5.2版本不支持匿名函数,如何在PHP5.2版本上使用类似function()use()的PHP闭包函数 目前我的代码如下所示如何在PHP5.2版本上使用类似function()use()的PHP闭包函数?,php,function,Php,Function,PHP5.2版本不支持匿名函数,如何在PHP5.2版本上使用类似function()use()的PHP闭包函数 目前我的代码如下所示 $this->init(function() use($taxonomy_name, $plural, $post_type_name, $options) { // Override defaults with user provided options $options = array_merge( array(
$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);
});
我假设您是因为早期的值绑定而请求“use”指令的,对吗? 例如,您可以使用“create function”并在其中插入一些静态变量和创建时的值
$code = '
static $taxonomy_name = "'.$taxonomy_name.'";
static $plural = "'.$plural.'";
static $post_type_name = "'.$post_type_name.'";
static $options = json_decode("'.json_encode($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);
';
$func = create_function('', $code);
这样做应该可以:
$this->init(create_function('','
$taxonomy_name = '.var_export($taxonomy_name,TRUE).';
$plural = '.var_export($plural,TRUE).';
$post_type_name = '.var_export($post_type_name,TRUE).';
$options = '.var_export($options,TRUE).';
$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开始就支持匿名函数,请参阅 您可以使用
create_函数
,但您应该避免使用该函数(例如,因为),它实际上与eval相同。。。一个坏的存储模块,您将使您的源易受攻击。它也在运行时进行评估,而不是在编译时进行评估,这可能会降低性能,并可能在包含文件的中间造成致命错误。
而不是在某处声明函数,它会更有效。
只需在工具栏上使用代码按钮(<代码> {} /代码>按钮)来格式化代码。我个人认为“代码> CREATEY函数(连同