调用未定义的函数错误,甚至定义了函数

gee*_*ere 0 php runtime-error

我有一个会话安全的文件"admin.php".这是登录后的默认页面.这引发了以下错误:

致命错误:在第14行的C:\ xampp\htdocs\cd-website\cms\admin.php中调用未定义的函数listPages()

<?php
/*
* initialize session for admin
*/
session_start();

if(isset($_SESSION['admin_user']))
{
    require_once '../cms/config.php';

    $action = isset($_POST['action']) ? $_POST['action'] : '';

    if ($action == null)
    {
        listPages();
        exit;
    }

    /*
     * using switch for choosing function
     */
    switch($action)
    {
        case 'ManagePages':
            ManagePages();
            break;
        case 'listUsers':
            listUsers();
            break;
        case 'orderList':
            listOrders();
            break;
        case 'listBanner':
            listBanners();
            break;
        case 'NewsletterUser':
            NewsletterUsers();
            break;
        case 'Newsletter':
            Newsletter();
            break;
        case 'listQuestion':
            listquestions();
            break;
        case 'testinomial':
            listTesti();
            break;
        default:
            listPages();
    }

    /*
     * different function for different tasks
     */
    function ManagePages()
    {
        listPages();
    }

    function listUsers()
    {
        // include listusers.php here. required rows is in listusers.php file
    }

    function listOrders()
    {
        // include listorders.php here
    }

    function  listBanners()
    {
        // include listbanners.php here
    }

    function  NewsletterUsers()
    {
        // include listNUsers.php here
    }

    function listquestions()
    {
        // include listquestions.php here.
    }

    function listTesti()
    {
        // include listTesti.php here.
    }

    function Newsletter()
    {
        //  include newsletter.php
    }

    function listPages()
    {
        //  include listPages.php here
    }
}
else
{
    header("Location:index.php");
}
Run Code Online (Sandbox Code Playgroud)

当我尝试解决第14行的错误时,它会在listPages()存在的每一行显示错误.

为什么会这样?请帮忙!

Alv*_*ong 8

你在一个if块中声明了一个函数,这可能很棘手.根据http://www.php.net/manual/en/functions.user-defined.php,条件函数,它们在执行到达函数定义之前将不可用,但如果将函数放在外面,则序列无关紧要,整个脚本都可以使用该功能.

<?php
bar(); // OK
function bar()
{
  echo "I exist immediately upon program start.\n";
}

if (TRUE) {
  foo(); // Fails because `foo` isn't defined yet.
  function foo()
  {
    echo "I don't exist until program execution reaches me.\n";
  }
}
Run Code Online (Sandbox Code Playgroud)

请查看http://codepad.org/EMW3kzqC.

因此,解决方案是在if块外部声明函数,或将它们放在您使用它的位置上方(如果它确实需要驻留在if块内).