WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am quite probably going about this the wrong way, but I cant figure out how to rectify this.... would be great if someone could point me in the right direction. happy to provide more details if required. My issue is as follows:

I have a parent class, a child class that needs to access some of the parent variables, and a page with a do_action call like so

/****plugin ***/
class PARENT(

    protected $pluginVersion;
    protected $pluginOptions;

    function __construct() {
        $this->pluginOptions = get_option('option_name',0);

        add_action('choose_gateway', array( $this, 'choose_gateway'));

    function choose_gateway(){
        /**output some dropdown, select, whatever*/
        echo $str;

class CHILD extends PARENT{
        function __construct() {

            $this->gatewayName = 'Cash on Delivery';
            /**plus some more other vars*/


                here i want to have access/use the variables set in the parent (i.e. $this->pluginOptions)
                initially i thought i can just use

                problem is though - if i do that - the "choose_gateway" action in parent is called twice....


                add_action('some_child_action', array($this,'some_child_action'));
        function some_child_action(){


        /**some more functions**/

/**** page***/
    /*echo other stuff */


    /*more other stuff */

the problem I have is as I've commented in the code above. I assum I would have to move the "add_action" call out of the parent construct , but I'm not sure, and if so don't know where to put it for it being still available via the "do_action" call ...

share|improve this question
up vote 0 down vote accepted

I think your problem is the pattern. You should not create a child class of your controller (the main class), and the controller should not handle action callbacks.

Let the controller assign the callbacks to actions. The action handlers should be separate classes.

Basic example:

namespace WPSE;

add_action( 'plugins_loaded', function() {

    new Controller;

class Controller
    public function __construct()
        $this->pluginVersion = '1.5';
        $this->pluginOptions = get_option('option_name',0);
        $gateway = new Gateway( $this->pluginVersion, $this->pluginOptions );

        add_action( 'choose_gateway', array ( $gateway, 'choose_gateway' ) );

        $cash = new Cash( $this->pluginVersion, $this->pluginOptions );
        add_action( 'get_cash', array ( $cash, 'get_cash' ) );

class Action_Handler
    public function __construct( $version, $options ) {}

class Gateway extends Action_Handler

    public function choose_gateway() {}

class Cash extends Action_Handler
    public function get_cash() {}
share|improve this answer
yeah, i figured it's something like that . just trying to get to grips with oop things and am still missing a lot of knowledge on that front. Having said that I find it always easier trying to learn by actually doing things than just reading some abstract code in some book (or wherever). In the meantime, I have found another way that kind of works but is nowhere near as elegant as the example you layed out above. So thank you for pointing me in the right direction. I'll mark your suggestion as the answer as it will no doubt take me a few days to tear it all apart and put it back together – olly Jun 14 '13 at 13:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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