Using PHP5 magic in Moodle: good idea, or bad idea?

Re: Using PHP5 magic in Moodle: good idea, or bad idea?

by Hubert Chathi -
Number of replies: 0
IMHO, __get and __set magic shouldn't be used unless the fake field is meant to look and smell more or less like a field (which means that there shouldn't be any major (unexpected) side effects). So it may be OK for context, but might not be for other fields (e.g. I'm not too sure about get_bodyclasses). But that's just my 2c.

In terms of PHPdoc, you'll probably want to use the property tag: http://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_tags.property.pkg.html (I don't know how xref deals with that tag, and it probably doesn't help with autocomplete.)

As far as lazy loading goes, this is probably not relevant for this case here, but FWIW, you can do something like this:
$getmethod = 'get_' . $field;
if (method_exists($this, $getmethod)) {
$this->$field = $this->$getmethod();
return $this->$field;
} else {
throw new coding_exception('Unknown field ' . $field . ' of $PAGE.');
}
With this code, the first time 'field' is accessed, if it isn't already an existing field, getmethod will be called and 'field's value will be set. Subsequent accesses will just get the field directly.