function forum_set_display_mode($mode=0) { global $USER, $CFG;
if ($mode) { $USER->mode = $mode; } else if (empty($USER->mode)) { $USER->mode = $CFG->forum_displaymode; } }
5. 语句块必须总是使用大括号(即便是只有一行)。Moodle使用如下风格:
if ($quiz->attempts) { if ($numattempts > $quiz->attempts) { error($strtoomanyattempts, "view.php?id=$cm->id"); } }
6. 字符串应当尽可能用单引号定义以提高速度。
$var = 'some text without any variables'; $var = "with special characters like a new line \n"; $var = 'a very, very long string with a '.$single.' variable in it'; $var = "some $text with $many variables $within it";
7. 实用的注释应当尽可能填写,用以解释代码流程和函数与变量的功能。
每个函数和类都应该使用流行的编写,以便自动生成代码文档。
内嵌注释应使用 // 风格,并且整齐布局,使其能融入代码中并和代码对齐。
/** * The description should be first, with asterisks laid out exactly * like this example. If you want to refer to a another function, * do it like this: {@link clean_param()}. Then, add descriptions * for each parameter as follows. * * @param int $postid The PHP type is followed by the variable name * @param array $scale The PHP type is followed by the variable name * @param array $ratings The PHP type is followed by the variable name * @return mixed */ function forum_get_ratings_mean($postid, $scale, $ratings=NULL) { if (!$ratings) { $ratings = array(); // Initialize the empty array if ($rates = get_records("forum_ratings", "post", $postid)) { // Process each rating in turn foreach ($rates as $rate) { ....etc
In order to guarantee follow these simple rules about the use of the AS keyword (only if you need table/colum aliases, of course):
不要将关键字AS用于表格别名。
要将关键字AS用于字段别名。
不要创建UNIQUE KEY(限制),用UNIQUE INDEX来代替它。 In the future, if
we decide to add referential integrity to Moodle and we need UNIQUE
KEYs they will be used, but not now. Please note that the XMLDB editor
allows you to specify both XMLDB-only UNIQUE and FOREIGN constraints
(and that's good, in order to have the XML well defined) but only
underlying INDEXes will be generated.
Those XMLDB-only UNIQUE KEYs (read previous point) only must be defined if such field/fields are going to be the target for some (XMLDB-only too) FOREIGN KEY. Else, create them as simple UNIQUE INDEXes.