Skip to content
Advertisement

PHP Classes: Should I use 2 classes or 1?

I am currently trying to make a class that deals with a survey in PHP (Drupal). So far I have this:

class Survey{

  public $id;
  public $uid;
  public $rate;
  public $reason;
  public $complete;
  public $opinion;

  public function save(){
    drupal_write_record('survey', $this);
  }
}

This is great, I can create a new instance of Survey, set the properties, and call save on it.

However, I also want to be able to have methods that retrieve the results of these surveys from the DB and act on them, giving me various numbers. It doesn’t feel right to have those methods in the class Survey, because they are actually multiple Surveys. However they are related, and may also return instances of Survey so not sure if they should be a completely separate class. What is the best thing to do here?

BTW I don’t care if its a Drupally answer.

Thanks, Amshad

Advertisement

Answer

As with anything, there are many approaches with their own advantages and disadvantages.

My (current) preference is to combine class methods (static) and object methods for handling groups of objects and individual objects, respectively.

Consider the following code:

class Survey {
    public $id;
    public $uid;
    public $rate;
    public $reason;
    public $complete;
    public $opinion;

    public function save()
    {
        drupal_write_record('survey', $this);
    }
    /**
     * Loads survey from secondary storage
     * 
     * @param string $id Unique surevy ID
     */
    public function load( $id ) {
        // loads a survey from secondary storage
    }
    /**
     * Returns report of survey results.
     * 
     * @param array $surveys array of surveys to process. If not passed or NULL,
     *     The whole set of completed surveys will be processed.
     * @return string HTML Formatted report
     */
    public static function compile_results( $surveys = NULL )
    {
        if( empty( $surveys ) )
        {
            $surveys = self::get_completed_results();
        }
        foreach( $surveys as &$survey )
        {
            // process an individual survey, possibly aggregating it
        } 
    }
    /**
     * Retreives completed surveys from secondary storage.
     * 
     * @return array  Array of completed Survey objects
     */
    public static function get_completed_surveys()
    {
        $surveys = array();
        // Select all surveys from secondary storage
        $survey_rows = array(); //replace with code to get surveys
        foreach( $survey_rows as $survey_row )
        {
            $survey = new Survey();
            $survey['id']       = $survey_row['id'];
            $survey['uid']      = $survey_row['uid'];
            $survey['rate']     = $survey_row['rate'];
            $survey['reason']   = $survey_row['reason'];
            $survey['complete'] = $survey_row['complete'];
            $survey['opinion']  = $survey_row['opinion'];

            $surveys[] = $survey;
        }
        return $surveys;
    } 
}

You can use the static methods to work with groups of objects. You could even have a static array holding a reference to each loaded survey.

Another option is to have a “Surveys” class whose purpose is to work with groups of surveys. The previous approach feels cleaner to me.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement