在 php 文件中添加函数 get 调用未定义函数异常

add function in php file get Call to undefined function exception

我是 php 的新手,我正在尝试在删除 mybb 论坛之前备份 post。

我编辑inc/class_moderation.php添加了下面的函数backup_post,但是当调用函数backup_post 抛出

Call to undefined function backup_post() in /membri/myforum/inc/class_moderation.php on line 485

function delete_post($pid)
{
    global $mybb, $db, $cache, $plugins;

    $pid = $plugins->run_hooks("class_moderation_delete_post_start", $pid);
    // Get pid, uid, fid, tid, visibility, forum post count status of post
    $pid = intval($pid);
    $query = $db->query("
        SELECT p.pid, p.uid, p.fid, p.tid, p.visible, f.usepostcounts, t.visible as threadvisible, message, p.username as p_username, p.subject, p.dateline
        FROM ".TABLE_PREFIX."posts p
        LEFT JOIN ".TABLE_PREFIX."threads t ON (t.tid=p.tid)
        LEFT JOIN ".TABLE_PREFIX."forums f ON (f.fid=p.fid)
        WHERE p.pid='$pid'
    ");
    $post = $db->fetch_array($query);
    // If post counts enabled in this forum and it hasn't already been unapproved, remove 1
    if($post['usepostcounts'] != 0 && $post['visible'] != 0 && $post['threadvisible'] != 0)
    {
        $db->update_query("users", array("postnum" => "postnum-1"), "uid='{$post['uid']}'", 1, true);
    }

    if(!function_exists("remove_attachments"))
    {
        require MYBB_ROOT."inc/functions_upload.php";
    }

    // Remove attachments
    remove_attachments($pid);

    //Backup the post
    backup_post($pid);
    // Delete the post
    $db->delete_query("posts", "pid='$pid'");

    // Remove any reports attached to this post
    $db->delete_query("reportedposts", "pid='$pid'");

    $num_unapproved_posts = $num_approved_posts = 0;
    // Update unapproved post count
    if($post['visible'] == 0 || $post['threadvisible'] == 0)
    {
        ++$num_unapproved_posts;
    }
    else
    {
        ++$num_approved_posts;
    }
    $plugins->run_hooks("class_moderation_delete_post", $post['pid']);

    // Update stats
    $update_array = array(
        "replies" => "-{$num_approved_posts}",
        "unapprovedposts" => "-{$num_unapproved_posts}"
    );
    update_thread_counters($post['tid'], $update_array);

    // Update stats
    $update_array = array(
        "posts" => "-{$num_approved_posts}",
        "unapprovedposts" => "-{$num_unapproved_posts}"
    );

    update_forum_counters($post['fid'], $update_array);

    return true;
}

function backup_post($pid)
{
    global $mybb, $db, $cache, $plugins;

    // Get pid, uid, fid, tid, visibility, forum post count status of post
    $pid = intval($pid);
    $query = $db->query("
        SELECT p.pid, p.uid, p.fid, p.tid, p.visible, f.usepostcounts, t.visible as threadvisible, message, p.username as p_username, p.subject, p.dateline
        FROM ".TABLE_PREFIX."posts p
        LEFT JOIN ".TABLE_PREFIX."threads t ON (t.tid=p.tid)
        LEFT JOIN ".TABLE_PREFIX."forums f ON (f.fid=p.fid)
        WHERE p.pid='$pid'
    ");
    $post = $db->fetch_array($query);

    $deletedpost = array(
            "username_delete" => $mybb->user['username'],
            "username" => $post['p_username'],
            "subject" => $post['subject'],
            "message" => $post['message'],
            "dateline" => TIME_NOW,
            "post_dateline" => $post['dateline'],
        );
    $db->insert_query("posts_deleted", $deletedpost);

    //14-06-20016
    //ob_start();
    //var_dump($mybb);
    //$data = ob_get_clean();
    //$fp = fopen(MYBB_ROOT."myText.txt","wb");
    //fwrite($fp,$data);
    //fwrite($fp, "cancellato da ".$mybb->user['username']);
    //fwrite($fp, "autore ".$post['p_username']);
    //fwrite($fp, "messaggio ".$post['message']);


    /*
    foreach ($post as $key => $value) {
        fwrite($fp, $key );
    }
    */
    //fclose($fp);



    return true;
}

您正在向 class 添加函数(然后我们倾向于将它们称为 "methods" 而不是 "functions",但这并不重要)。

当你想调用一个class方法时,你需要像$class_instance->method_name()那样调用它而不是通常的method_name(),它适用于"plain"函数。

当您从同一 class 中的另一个方法调用方法时,您使用 $this 获取相同的 class 实例。

因此,如果您更改此行:

backup_post($pid);

至:

$this->backup_post($pid);

应该可以。