1

我有一个受会话保护的文件“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");
}

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

为什么会这样?请帮忙!

4

5 回答 5

10

You declared a function inside a if block, which can be tricky. According to http://www.php.net/manual/en/functions.user-defined.php, conditional functions, they will not be available until the execution reaches the function definition, but if you place the function outside, the sequence doesn't matter and the function is available throughout the whole script.

<?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";
  }
}

And please check http://codepad.org/EMW3kzqC.

So the solution is to declare the function outside the if block, or place them above the location where you use it (if it really needs to reside inside the if block).

于 2012-10-23T05:25:50.457 回答
0

I'm not exceptionally familiar with php, but it looks like you're defining your functions only within the big if block there. You might be running into the way php handles scope.

Try placing your function definitions outside of the if block and see what happens?

于 2012-10-23T05:29:21.017 回答
0

在调用之前检查您的功能是否已设置。在 PHP 中,您必须在调用函数之前声明它们。

您的错误清楚地表明,当您调用listPages()此函数时还不存在。

我确定您是否紧随其后:

if(isset($_SESSION['admin_user']))
{

该函数的声明

function listPages()
{
    //Do something here
}

您的错误将消失。

我可以看到您正在尝试在结构代码中使用函数,就像它们在某些类(面向对象的代码)中一样,不幸的是 PHP 不能那样工作。

于 2013-01-15T14:03:34.807 回答
0

这里有两种解决方案,一种是使用类,上述方法如果您使用上述方法,则应在调用该函数之前定义函数。

所以

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

after that call listPages()
于 2012-10-23T05:23:45.340 回答
-1
<?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;
    }


    /*Functions */
    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
    }


    /*
     * 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
     */

}
else
{
    header("Location:index.php");
}
于 2012-10-23T05:34:02.023 回答