php 5.2.5 我编写了函数来从MySQL数据库中获取模块。
function getModules($courses, $mod) {
global $DB;
$result = array();
foreach ($courses as $value) {
$value->mods = array();
$value->count = 0;
$temp = $DB->get_records_sql("
SELECT q.*, cm.idnumber as cmidnumber, q.course as courseid
FROM {modules} m
JOIN {course_modules} cm ON m.id = cm.module
JOIN {".$mod."} q ON cm.instance = q.id
WHERE m.name = '".$mod."' AND cm.course = ?", array($value->id));
foreach ($temp as $vS) {
$value->mods[] = $vS;
$value->count++;
}
$result[] = $value;
}
return $result;
}
尝试获取某些类型的模块(to_debug只是关于var_dump的一种包装)
$learningScorm = getModules($learning, 'scorm');
to_debug($learningScorm); // in debug I can see right values.
echo '<br><br><br>';
$learningLesson = getModules($learning, 'lesson');
to_debug($learningScorm);// in debug I see what value of $learningScorm is replaced by value of $learningLesson
$testingQuiz = getModules($testing, 'quiz');
$labAssignment = getModules($lab, 'assignment');
我无法理解为什么这种替换正在发生 如果你对这种行为有一些提示,请给我。
如果我评论这些行
$value->mods = array();
$value->count = 0;
然后$ learningScorm从$ learningScorm和$ learningLesson求和模块。似乎......似乎在功能O_O期间$课程不是本地的。我不知道该怎么想。
答案 0 :(得分:0)
getModules是否返回引用?在这种情况下,两个变量都可能指向相同的信息。可能发生的事情的简化示例:
var $global = 'x';
function getModules($a){
global $global;
$global = $a;
return &$global;
}
// $a becomes 'foo', or actually a reference to $global, which now
// has the value 'foo'
$a = getModules('foo');
// $b also is a reference to $global, which now has the value 'bar'.
// Therefor, both $a and $b will show the value 'bar.
$b = getModules('bar');
答案 1 :(得分:0)
$ courses是本地的,但$ course包含的对象是真实对象。 通过改变$ value,物品也会发生变化。