public function load_optional_fields() {
        global $DB;
        foreach ($this->optional_fields as $field=>$default) {
            if (property_exists($this, $field)) {
                continue;
            }
            if (empty($this->id)) {
                $this->$field = $default;
            } else {
                $this->$field = $DB->get_field($this->table, $field, array('id', $this->id));
            }
        }
    }
public function load_optional_fields() {
        global $DB;
        foreach ($this->optional_fields as $field=>$default) {
            if (property_exists($this, $field)) {
                continue;
            }
            if (empty($this->id)) {
                $this->$field = $default;
            } else {
                $this->$field = $DB->get_field($this->table, $field, array('id', $this->id));
            }
        }
    }
public function load_optional_fields ( ) { global $ DB ; foreach ( $ this -> optional_fields as $ field => $ default ) { if ( property_exists ( $ this , $ field ) ) { continue ; } if ( empty ( $ this -> id ) ) { $ this -> $ field = $ default ; } else { $ this -> $ field = $ DB -> get_field ( $ this -> table , $ field , array ( 'id' , $ this -> id ) ) ; } } }
Makes sure all the optional fields are loaded.

If id present (==instance exists in db) fetches data from db.
Defaults are used for new instances.
Makes sure all the optional fields are loaded .