LPH
Well-known member
I'm trying to wrap my head around a few concepts and am running up against a wall. There is a class which pulls the same option array in each method. It seems more appropriate to call the option array once but cannot figure out the most efficient method; actually, I cannot figure out a single way to do it.
Here is the old code (many methods stripped out for ease of understanding).
The $xenword_options variable is repeated over and over again in the different methods within this class.
I tried to call the variable public using the following; as well as changing methods to nonstatic.
An error is returned using $this when not in object context.
Is there an obvious way to achieve DRY in this class?
Here is the old code (many methods stripped out for ease of understanding).
PHP:
class XenWord_XF_Users {
public function __construct() {
//
}
public static function check_options() {
$xenword_options = XenWord::getOptions();
if ( $xenword_options['use_add_users'] === '1' ) {
/** Determine role of XF User based on secondary user groups */
self::check_role();
} else {
return false;
}
}
public static function check_role() {
$visitor = XenWord::getVisitor();
$xenword_options = XenWord::getOptions();
if ( $xenword_options['use_adv_mapping'] === '1' ) {
// This is a string array separated by commas
$userGroupIds = $visitor['secondary_group_ids'];
$userGroups = explode(',', $userGroupIds);
foreach ( $userGroups as $userGroupId ) {
$xf_role = 'xf_role_' . $userGroupId;
$wp_role = $xenword_options[$xf_role];
}
// Zend_Debug::dump($wp_role);
} else {
$wp_role = $xenword_options['wp_default_role'];
}
/** Check if the user is already in the database */
self::check_user( $wp_role );
}
}
new XenWord_XF_Users();
The $xenword_options variable is repeated over and over again in the different methods within this class.
I tried to call the variable public using the following; as well as changing methods to nonstatic.
Code:
public $xenword_options;
public function __construct() {
$this->xenword_options = XenWord::getOptions();
}
An error is returned using $this when not in object context.
Is there an obvious way to achieve DRY in this class?