2012-08-11 64 views
0

可能重复意外T_FUNCTION:
How can I use PHP closure function like function() use() on PHP 5.2 version?语法错误,在 “使用” 操作

我试图给正在运行PHP 5.2的服务器上运行此。

function add_post_type($name, $args = array()) { 
add_action('init',function() use($name, $args) { 

    // execute custom post type code here 

}); 
}; 

第二行抛出一个意外T_FUNCTION错误,我怀疑它的“使用”操作的原因。有人可以帮我指出我如何重写这个函数来运行在PHP 5.2中?

+0

我想这是因为你没有在'function()'后面指定大括号。但是你究竟想要做什么? – Hafiz 2012-08-11 19:32:40

+0

@Hafiz我正在做一个自定义帖子类型帮助函数,把我的function.php文件放在我的wordpress主题中。使CPT更快,更方便。前段时间跟随了这个教程。至于te花括号,它是在“使用($ name,$ args)”之后的语句,因为我知道它使变量可用于下面的函数。无论如何,它运行良好,我的MAMP有PHP 5.3。我的产品有5.2 – 2012-08-11 20:40:49

回答

1

看到这个函数: -

/*添加新贴类型*/

function wpse54191_plugin_init() { 
add_post_type('Netherlands', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments') 
)); 
} 
add_action('init', 'wpse54191_plugin_init'); 

/* Add Post Type */ 
function add_post_type($name, $args = array()) { 
    if (!isset($name)) return; 

    $name = strtolower(str_replace(' ', '_', $name)); 
    $args = array_merge(
     array(
      'label' => 'Members ' . ucwords($name) . '', 
      'labels' => array('add_new_item' => "Add New $name"), 
      'singular_name' => $name, 
      'public' => true, 
      'supports' => array('title', 'editor', 'comments'), 
     ), 
     $args 
    ); 

    register_post_type($name, $args); 
} 
+0

这对你来说非常有帮助 – 2012-08-11 19:33:13

+0

真棒这看起来不错,会马上试试 – 2012-08-11 20:43:00

0

这个答案似乎为你想在PHP 5.2做什么好的解决办法:将匿名函数到用户定义的功能。

Converting Code with Anonymous functions to PHP 5.2

祝你好运!并尝试升级你的PHP版本:P

+0

谢谢,看起来我必须赶上那个话题。我在一个没有访问WHM或shell的共享托管软件包中,所以我不认为我可以自己升级它。 – 2012-08-11 20:44:36