You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

删除Moodle作业活动时触发‘Value of type null is not callable’错误求助

解决删除作业活动时的“Value of type null is not callable”错误

问题描述

删除作业活动时触发错误:

Value of type null is not callable

错误发生在调用$assignment->delete_instance();的位置,已确认外部传入的$assignment是有效对象,但执行到全局函数assign_delete_instance内部时出错。

问题根源

查看assign_delete_instance函数代码:

function assign_delete_instance($id) {
$assignment->delete_instance();//this is where the execution stopped
}

这里的$assignment变量未在函数内部初始化,属于未定义变量,默认值为null,调用null->delete_instance()自然会抛出“null不可调用”的错误。

另外代码中存在方法名复用为变量的情况:在remove_course_contents函数里,$moddelete = $modname .'_delete_instance';将函数名赋值给变量,这本身不是直接错误,但结合assign_delete_instance内部的变量缺失,共同导致了问题。

修复方案

修改assign_delete_instance函数,先通过传入的$id实例化有效的assign对象,再调用其方法:

function assign_delete_instance($id) {
    // 实例化assign对象(遵循Moodle标准实例化方式)
    $assignment = new assign(null, $id, null);
    $assignment->delete_instance();
}

也可以先校验ID有效性再实例化:

function assign_delete_instance($id) {
    global $DB;
    $assignrecord = $DB->get_record('assign', array('id' => $id));
    if (!$assignrecord) {
        throw new \moodle_exception('invalidassignmentid', 'assign');
    }
    $assignment = new assign(null, $id, null);
    $assignment->delete_instance();
}

额外注意

确保调用链中$cm->modinstance传递的是有效的作业ID,避免传入无效值导致实例化失败。

内容的提问来源于stack exchange,提问作者Andrewboy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 02:47:38