致命错误:无法重新声明getteampoints()(之前在声明中声明)

时间:2013-04-10 10:37:50

标签: php function

获得以下函数,之前未在任何地方声明getteampoints值。试图遵循其他redececlare错误问题,但没有工作。我该如何解决这个问题?

function getTeamPoints($team)
    {
        $query = mysql_query("SELECT * FROM Team_comp WHERE t_id='$team'");
        $team_array = array();

        while($a = mysql_fetch_array($query))
        {
            $team_array = array(            'home_won'  =>  $a['home_win'],
                                            'home_draw' =>  $a['home_tie'],
                                            'home_lost' =>  $a['home_lost'],
                                            'away_won'  =>  $a['away_win'],
                                            'away_draw' =>  $a['away_tie'],
                                            'away_lost' =>  $a['away_lost'],
                                            'home_games'=>  $a['home_games'],
                                            'away_games'=>  $a['away_games']);
        }

        return $team_array;
    }

    function calculateTeamPoints($team, $type)
    {
        $teamPts = getTeamPoints($team);

        if($type == 'home')
        {
            $homem = $teamPts['home_games'];
            $homew = $teamPts['home_won'];
            $percent = ($homew * 100) / $homem;

            $remaining = $homem - $homew;

            $per = ($remaining * 100) / $homem;
            $percent += $per / 2;
        }
        elseif($type == 'away')
        {
            $homem = $teamPts['away_games'];
            $homew = $teamPts['away_won'];
            $percent = ($homew * 100) / $homem;

            $remaining = $homem - $homew ;

            $per = ($remaining * 100) / $homem;
            $percent += $per / 2;
        }

        return $percent;
    }

    function getpercent($hometeamid, $awayteamid)
    {
        $hometeampts = calculateTeamPoints($hometeamid, 'home');
        $awayteampts = calculateTeamPoints($awayteamid, 'away');


        $homepercent = floor(($hometeampts - $awayteampts) + 50);
        $awaypercent = 100-$homepercent;


    }

    //demo
    getpercent($hometeamid, $awayteamid);
    ?>

2 个答案:

答案 0 :(得分:3)

在IF条件下推送函数getTeamPoints ...

if(!function_exists('getTeamPoints')){ 
    function getTeamPoints()....

}

不可能多次声明1个功能!

如果你声明它超过1次,你必须写不同的名字,如果你只是包含那个文件超过1次(这是错误的..)这个IF函数存在检查将正常工作。

答案 1 :(得分:0)

查看函数重新声明错误是您第二次直接或间接包含同一文件的标志。

除了错误消息本身,这表示应用程序工作流程中存在问题。

最直接的解决方案是找出第二次加载功能的时间和原因,然后修复工作流程。

您可以通过以下方式找到更多信息:

if(!function_exists('getTeamPoints')) {
    throw new Exception('Function getTeamPoints() already declared.');
}

include('getteampoints.php');

抛出一个异常不仅可以让你以后捕获它(不可能发生致命错误),但它也会显示一个回溯,这样你就可以更容易地找到文件执行的位置(以及为什么)时间。

如果你不确定该结果,你还可以暂时否定条件并在第一次加载函数时抛出异常。

相关问题