【问题标题】:PHP Complaing on redeclare in same fil, but only on declare. Bug?PHP 在同一文件中重新声明时抱怨,但仅在声明时。漏洞?
【发布时间】:2011-09-21 19:34:40
【问题描述】:

我收到以下错误消息:

   [Wed Sep 21 21:19:46 2011] [error] [client 127.0.0.1] PHP Fatal error:  Cannot redeclare db_get_groups() 
(previously declared in /Library/WebServer/Documents/SMICAdmin/databasescripts/db_get_groups.php:4) 
in /Library/WebServer/Documents/SMICAdmin/databasescripts/db_get_groups.php on line 24, 
referer: http://localhost/SMICAdmin/index.php

如您所见,错误消息抱怨同一个函数 db_​​get_groups() 在同一个脚本中出现了两次。但事实并非如此,整个文件都包含在这里(db_get_groups.php):

<?php

    function db_get_groups($dbconnection){  //Line 4

        $query = "SELECT id FROM groups";
        $result = mysqli_query($dbconnection, $query);

        $rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
        $grouplist = Array();

        foreach ($rows as $key => $value) {
            error_log(" Value -> ".$value['id']);
            $grouplist[] = $value['id'];

        }

    return $grouplist;
    } //Line 24
    ?>

在整个项目中搜索“db_get_groups”时,我只能找到下面唯一的声明,并且该文件包含在其他两个文件中以供使用。

我确实尝试查找是否以某种方式多次导入文件,但找不到。

有什么问题,我该如何解决?感觉这个真的连线了……

【问题讨论】:

  • 您确定您的文件没有在 index.php 中包含两次吗?
  • 要了解@AlanMoore 所说的内容,请尝试include_once(db_get_groups.php)
  • function_exists(db_get_groups); 的结果是什么?

标签: php macos apache function-declaration


【解决方案1】:

最重要的是,您看到该错误消息的唯一方法是该函数被定义了两次。发生这种情况的常见方式有两种。你应该仔细检查这两个:

  1. 另一个文件声明了函数db_get_groups
  2. db_get_groups.php 包含多次。考虑使用include_once / require_once 而不仅仅是include / require

【讨论】:

  • 我采取了简单的方法,只是将所有内容都定义为 include_once。不知道是什么问题。在此之后有很多重新声明。从没有到很多……奇怪。
【解决方案2】:

使用: require_once("db_get_groups.php");

或者, include_once("db_get_groups.php");

而不仅仅是要求或包含。

【讨论】:

    【解决方案3】:

    尝试删除该定义,并调用 db_get_groups(),查看错误日志中是否有任何提示。

    如果这不去任何地方,您是否可能不止一次地包含该文件?您是否将其他包含它的文件都包含在某处?

    你使用了 require/include 还是 require_once/include_once?尝试 *_once。

    是否有指向该文件的符号链接?如果是这样,您能否将它们也包括在内?

    最后,如果你在函数之外(在它之前)添加一个 log() 或 echo(),它可以帮助你。

    【讨论】:

      猜你喜欢
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多