5

I have something similar as here.

In a third party plugin class we have a function that I want to override by extending that class.

The original class function:

class Import_Facebook_Events_Facebook {
    public function get_location( $facebook_event ) {

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

What I tried, but this didn't work (no effect):

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?

The original Import_Facebook_Events_Facebook() class is instantiated from another class:

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; 
    }
}

And the above class is instantiated from a separate function:

function run_import_facebook_events() {
    return Import_Facebook_Events::instance();
}
5
  • 1
    I suppose you could try to override the instance after the fact. eg. Import_Facebook_Events::$instance = Import_Facebook_Events_Ext::instance(); ... otherwise you'd have to keep going back to what runs run_import_facebook_events to see if you can override at that top level.
    – majick
    Oct 18, 2017 at 8:15
  • @majick I tried that and I got a This page isn’t working. infopsi.md is currently unable to handle this request. HTTP ERROR 500 screen.
    – Yurié
    Oct 18, 2017 at 8:33
  • 1
    before to instanciate your new class, you have to stop the instanciation of the original class then you have to find where this appends.
    – mmm
    Oct 18, 2017 at 9:38
  • @mmm I added an answer, can you look at it?
    – Yurié
    Oct 18, 2017 at 10:08
  • @majick I added an answer, can you look at it?
    – Yurié
    Oct 18, 2017 at 10:09

1 Answer 1

4

Finally, I figured out how to solve my problem:

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?

1
  • 1
    good solve, looks like you could use that wherever you need to... it really just depends where you actually want to use $new_ife_events
    – majick
    Oct 18, 2017 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.