Registering and using actions which return results in a Plugin class

Some actions hooks need to be fired on specific events. Try this code:

class SpektrixPlugin {

    public function __construct(){
        add_action('init', array(&$this, 'init'));
    }

    public function spektrix_list_events($getPrices = FALSE) {
        $api = new SpektrixApiClient();
        return $api->getAllEvents($getPrices);
    }

    public function init(){
        add_action( 'spektrix_list_events', array ( $this, 'spektrix_list_events' ));
    }

 }

 $SpektrixEvents = new SpektrixPlugin();

I’ve tested this code and it works:

class SpektrixPlugin {

    public function __construct(){
        add_action('init', array(&$this, 'init'));
    }

    public function spektrix_list_events() {
        echo 'test';
    }

    public function init(){
        add_action( 'spektrix_list_events', array ( $this, 'spektrix_list_events' ));
    }

 }

 $SpektrixEvents = new SpektrixPlugin();

 //this print 'test', so it works.
 do_action('spektrix_list_events');

But …. I’ve been reading the WordPress documentation about do_action() and it says clearly that do_action() will call the function/method but it won’t return anything. Quoteing WordPress about do_action():

This function works similar to apply_filters() with the exception that
nothing is returned and only the functions or methods are called.

So, you should check the apply_filters() function which works in a similar way that do_action() but can return the value returned by the called method or look for another implementation. do_action() is not suitable to return values.

An example usgin apply_filters:

class SpektrixPlugin {

    public function __construct(){
        add_action('init', array(&$this, 'init'));
    }

    public function spektrix_list_events() {
        $myvar = "Hey, I'm works";
        return $myvar;
    }

    public function init(){
        add_filter( 'spektrix_list_events', array ( $this, 'spektrix_list_events' ));
    }

 }

 $SpektrixEvents = new SpektrixPlugin;

 $test = apply_filters('spektrix_list_events','');
 var_dump($test);

Anyway, I think that this approach used to get data is no the appropiate. Although apply_filters() will work to get a value, as in the above example, the function is specially designed to filter a value, not to get one. I think the best you can do is have a get method in your class used to get the value you want, then you can apply filters to that value or do actions with it.