Refactoring PHP Code to DRY

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).

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?
 
Instead of being a wet noodle, I started a project just for testing OOP. Here is code that has a public property that is an array. I just haven't figured out how to modify the getAge() to be a foreach. The variable name $age is probably poor at this point, too, but came from an example without an array.

PHP:
<?php

class User {

   public $visitor = array();

   public function __construct($age) {

      $this->visitor[] = $age;

      // var_dump($age);

      /**
       array (size=2)
           'Brad' => int 31
           'Jesse' => int 24
       */

   }

   public function getAge() {
     
      echo "Brad's Age is " . $this->visitor[0]['Brad'] . '<br />';
      echo "Jesse's Age is " . $this->visitor[0]['Jesse'] . '<br />';
   }

}

$sampleArray = array('Brad' => 31, 'Jesse' => 24);

$obj = new User($sampleArray);

$obj->getAge();

This outputs as expected but doesn't use the foreach as I'd like to learn.
 
An object is usually a single thing.

Let's say you have a user object the code would be..

$user1 = new User();

$user1->setAge(18);
$user1->setGender("male");
$user1->setName("Brad");

$user2 = new User();

$user2->setAge(18);
$user2->setGender("female");
$user2->setName("Jenifer");

$userArray = new array();

$userArray[] = $user1;
$userArray[] = $user2;

Then do your for each.


Maybe I'm picking you up wrong..
 
An object is usually a single thing.

Question 1
After fussing for quite some time, a good explanation might be to write: "I'm trying to understand how to use an associative array as a public property. "

But that might not be be the best wording either. I've now set up the development site for just testing different OOP ideas and the second post made includes an array being used. The next thing is to figure out how to simplify things so $this->visitor[0]['key] is not repeated over and over again throughout the class. Again, I'm not sure if this is the best wording.

Question 2
How are associative arrays included in the instantiation of an object? In the second post, I created a variable $sampleArray and used it. :unsure:

Question 3
In the second post, you will see a foreach loop is not used. I'm looking for examples of how to loop.

The complexity of this problem is most likely due to the rats nest of moving a procedural set of code to class OOP.

As always thank you for any help and guidance.
 
OMG ! It clicked.

PHP:
class XenWord_XF_Users {

   public $options;
   public $visitor;

   public function __construct() {
      $this->options = XenWord::getOptions();
      $this->visitor = XenWord::getVisitor();
   }

   public function check_options() {
      
      if ( $this->options['use_add_users'] === '1' ) {

         /** Determine role of XF User based on secondary user groups */
         $this->check_role();

      } else {
         return false;
      }
   }
}

This code works like a champion. Well, I stripped out 90% of the methods after this code but you get the drift. I'm a happy camper. The property sort of clicked and I understood how to get the array into the property through the construct.
 
Top Bottom