包装类:如何摆脱CALL_USER_FUNC_ARRAY()警告?

时间:2013-08-14 作者:Leandro Villagran

我已经安装了好几个wordpress,现在我正在尝试创建自己的小框架,以便更有效地创建和维护我的主题。我想做的是为wordpress提供的挂钩提供一个包装。

我想在我的应用程序中调用这样的挂钩

$_addThumb = function(){
add_theme_support(\'post-thumbnails\');
};

 $core->registerAction(array(\'tag\' => \'after_theme_setup\', 
                             \'callback\' => $_redirect
                             ));
在我的类中,我像这样处理逻辑(这是一个片段,整个代码如下)

    if (!empty($this -> _actions)) {
        foreach ($this->_actions as $action) {
            add_action($action[0], array($this, call_user_func($action[1])));
        }
    }
问题是,如果我像这样将$this关键字添加到add\\u操作调用中…

add_action($tag, array($this, $callback))  
…我收到以下警告:

警告:call\\u user\\u func\\u array()要求参数1是有效回调,第二个数组成员不是Z:\\Website\\wp includes\\plugin中的有效方法。php

如果我不这样添加它…

add_action($tag, $callback)
然后我收到通知:

未定义的偏移量:Z:\\Website\\wp includes\\plugin中的0。php

不过,在这两种情况下,一切都是可行的。我只是想了解为什么会发生这种情况,至少要消除警告。

以下是我的完整代码:

class Core {

/**
 * theme features
 */
protected $_features = array();
private $_actions = array();


/**
 * register extra functions so you can add actions to the theme
 *@param array mixed
 */
public function registerAction($args) {

    if (!is_array($args)) {
        $args = func_get_args();
    }
    if (count($args) < 2) {
        throw new InvalidArgumentException(sprintf(\'Not enough arguments(registerCallback needs at least two arguments): %s.\', print_r($args, true)));
    }
    $arg_list = array_values($args);

    /**
     * sets the default priority
     */
    if (!isset($arg_list[2])) {
        $arg_list[2] = 10;
    }
    /**
     * sets default args
     */
    if (!isset($arg_list[3])) {
        $arg_list[3] = 0;
    }

    if (!is_callable($arg_list[1])) {
        throw new Exception("callback not callable : Core:13");
    }

    $this -> _actions[] = $arg_list;

}

/**
 * TODO: development only
 */
public function get_actions() {
    echo "<pre>";
    print_r($this -> _actions);
    echo "</pre>";
}

/**
 * set theme features
 * \'post-formats\'
 * \'post-thumbnails\'
 * \'custom-background\'
 * \'custom-header\'
 * \'automatic-feed-links\'
 * \'menus\'
 * @param string (required) name of the feature to be added
 * @param array (optional) optional arguments
 */
public function set_feature($feature, $options = array()) {

    if (is_array($options) && !empty($options)) {
        $this -> _features[][\'args\'] = array($feature, $options);
    } else {
        $this -> _features[][\'name\'] = $feature;
    }
}

public function get_features() {
    return $this->_features;
}

function __construct() {

}

public function init($tag = \'\', $function = \'\') {
    add_action(\'after_setup_theme\', array($this, \'initial_setup\'));

    if (!empty($this -> _actions)) {
        foreach ($this->_actions as $action) {
            add_action($action[0], array($this, call_user_func($action[1])), $action[2], $action[3]);
        }
    }
}

public function initial_setup() {

    // check if we have ay features so we can add them to our theme
    if (!empty($this -> _features)) {
        foreach ($this->_features as $feature) {
            if (isset($feature[\'args\'])) {
                add_theme_support($feature[\'args\'][0], $feature[\'args\'][1]);
            } else {
                add_theme_support($feature[\'name\']);
            }
        }
    }
}

}
我的职能。php

$core = new Core();

$core->set_feature(\'automatic-feed-links\');

$core->set_feature(\'post-formats\', array(\'aside\',
                                         \'image\',
                                         \'link\',
                                         \'quote\',
                                         \'status\',
                                         \'gallery\',
                                         \'video\' ));
$_addThumb = function(){
    add_theme_support(\'post-thumbnails\');
};

$_sidebars = function() {
    register_sidebar( array(
        \'name\'          => __( \'Main Widget Area\', \'twentythirteen\' ),
        \'id\'            => \'sidebar-1\',
        \'description\'   => __( \'Appears in the footer section of the site.\', \'twentythirteen\' ),
        \'before_widget\' => \'<aside id="%1$s" class="widget %2$s">\',
        \'after_widget\'  => \'</aside>\',
        \'before_title\'  => \'<h3 class="widget-title">\',
        \'after_title\'   => \'</h3>\',
    ) );

    register_sidebar( array(
        \'name\'          => __( \'Secondary Widget Area\', \'twentythirteen\' ),
        \'id\'            => \'sidebar-2\',
        \'description\'   => __( \'Appears on posts and pages in the sidebar.\', \'twentythirteen\' ),
        \'before_widget\' => \'<aside id="%1$s" class="widget %2$s">\',
        \'after_widget\'  => \'</aside>\',
        \'before_title\'  => \'<h3 class="widget-title">\',
        \'after_title\'   => \'</h3>\',
    ) );
};

$_adminHead = function(){

echo \'<style>[for="wp_welcome_panel-hide"] {display: none !important; } #normal-sortables{display:none !important;}</style>\';

};

/** i don\'t get a notiche with this **/
$core->registerAction(array(\'tag\' => \'after_theme_setup\', \'callback\' => $_addThumb));

/** it works but i get this warning 
 * arning: call_user_func_array() expects parameter 1 to be a valid callback, second array member is not a valid method in Z:\\Armen Website\\wp-includes\\plugin.php on line
 */
$core->registerAction(array(\'tag\' =>\'admin_head\', \'callback\' => $_adminHead));
$core->registerAction(array(\'tag\' => \'widgets_init\', \'callback\' => $_sidebars));
$core->init();

2 个回复
SO网友:kaiser

正如你已经注意到的,$this 在PHP 5.4之前,将无法正常工作。你可以read this in the PHP wiki. 原因:

对于PHP 5.3$this 对闭包的支持被取消,因为无法就如何以理智的方式实现闭包达成共识。这个RFC描述了在下一个PHP版本中实现它可能采取的方法。

一个简单的解决方法是简单复制$this 首先,然后使用新的引用/变量:

$subject = clone $this;

SO网友:Leandro Villagran

我想出来了。问题是add\\u操作需要一个具有方法的对象。add_action($action[0], array($this, call_user_func($action[1]))); im使用$this关键字传递对象,但无法识别该方法,因为它是一个闭包,因此不在类的范围内。最好的解决方案是将闭包绑定到对象,但是im使用php 5.3和闭包绑定仅在版本5.4上可用。因此,我最终要做的是为所有闭包创建一个注册表,并将其附加和别名,以便每个闭包看起来像类的一个方法。给我这个想法的是这个答案https://stackoverflow.com/a/420030/1287608 这就是我最终做这件事的方式。请注意,在本例中,我是如何使用别名“loadAction”向注册表添加新方法的。$this->register($closure, "loadAction{$i}")

public function init($tag = \'\', $function = \'\') {

    add_action(\'after_setup_theme\', array($this, \'initial_setup\'));

    if (!empty($this -> _actions)) {
        $i = 0;
        foreach ($this->_actions as $action) {
            $this->register($action[1], "loadAction{$i}");
            $success = add_action($action[0], array($this, "loadAction{$i}"), $action[2], $action[3]);
        $i++;
        }
    }
}

结束

相关推荐

Problems with loop

我的索引中有这个循环。php: <?php if (have_posts()) : while (have_posts()) : get_template_part( \'post\' ); endwhile; endif; ?> 调用此模板<?php ?> <h2 id=\"post-<?php the_ID()