Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As with anything, there are many approaches with their own advantages and disadvantages.</p> <p>My (current) preference is to combine class methods (static) and object methods for handling groups of objects and individual objects, respectively.</p> <p>Consider the following code:</p> <pre><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 &amp;$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; } } </code></pre> <p>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.</p> <p>Another option is to have a "Surveys" class whose purpose is to work with groups of surveys. The previous approach feels cleaner to me.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload