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 …
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: