替换数组的值
php 5.2.5 I wrote the function to get modules by courseid from MySQL database.
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;
}
Try to get some type of modules (to_debug just kind of wrapper about 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');
I cannot understand why this replacing is happening If you have some hints about such behaviour, please give me it.
If I comment these lines
$value->mods = array();
$value->count = 0;
then $learningScorm is summing modules from $learningScorm and $learningLesson. It seems... Seems what $courses is not local during function O_O. I do not know what to think already.
$courses is local, but objects contained by $courses are real objects. By changing $value's really items is changing too.
Does getModules return a reference? In that case, both variables may point to the same information. Simplified example of what could have happened:
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');