1

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->pluginVersion='1.5';
        $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*/

            /**
                problem:

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

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

            ***/


            if(!is_admin()){
                add_action('some_child_action', array($this,'some_child_action'));
            }
        }
        function some_child_action(){

        }

        /**some more functions**/
}
?>


/**** page***/
<?php
    /*echo other stuff */

    do_action('choose_gateway');

    /*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 ...

0

1 Answer 1

0

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() {}
}
1
  • 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
    Commented Jun 14, 2013 at 13:17

Your Answer

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

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