重写插件类函数

时间:2017-10-18 作者:Iurie

我有一些类似于here.

在第三方插件类中,我们有一个函数,我想通过扩展该类来覆盖它。

原始类函数:

class Import_Facebook_Events_Facebook {
    public function get_location( $facebook_event ) {

        if ( !isset( $facebook_event->place->id ) ) {
            return null;
        }
        //other code here
}
我尝试了什么,但没有成功(没有效果):

class Import_Facebook_Events_Facebook_Ext extends Import_Facebook_Events_Facebook {
    public function get_location( $facebook_event ) {

        if ( !isset( $facebook_event->place->id ) ) {
            $facebook_event->place->id = \'\'; //added this line
            //return null;
        }
        //other code here
}

new Import_Facebook_Events_Facebook_Ext();

What\'s wrong here? How can I get the desired effect?

原件Import_Facebook_Events_Facebook() 类是从另一个类实例化的:

class Import_Facebook_Events{

    private static $instance;

    public static function instance() {
        if( ! isset( self::$instance ) && ! (self::$instance instanceof Import_Facebook_Events ) ) {
            self::$instance = new Import_Facebook_Events;

            self::$instance->facebook = new Import_Facebook_Events_Facebook();

        }
        return self::$instance; 
    }
}
上述类是从单独的函数实例化的:

function run_import_facebook_events() {
    return Import_Facebook_Events::instance();
}

1 个回复
最合适的回答,由SO网友:Iurie 整理而成

最后,我想出了如何解决我的问题:

class Import_Facebook_Events_Facebook_Ext extends Import_Facebook_Events_Facebook {

    function get_location( $facebook_event ) {

        if ( !isset( $facebook_event->place->id ) ) {
            $facebook_event->place->id = \'\';
            //return null;
        }
        //other code here

        return $event_location;
    }
}

$new_ife_events = run_import_facebook_events();
$new_ife_events->facebook = new Import_Facebook_Events_Facebook_Ext();

A question: the last two code lines must be included in a function/action?

结束

相关推荐

Functions.php中的入队样式

这对我没用functions.php: if( is_page_template( \'template-flat.php\' ) ) { function flatsome_scripts() { wp_enqueue_style( \'flatsome-style\', get_template_directory_uri() .\'/flatash/css/foundation.css\', array(), \'2.1\', \'all\'); }