【问题标题】:Cannot redeclare function php [duplicate]无法重新声明函数php [重复]
【发布时间】:2012-06-11 10:31:42
【问题描述】:

我有一个名为 parseDate 的函数,但是当我在我的 php 页面(它是一个 joomla 组件页面)上调用它时,我收到致命错误:无法重新声明 parsedate()(之前在模板/ja_zeolite/assets/functions.php 中声明: 2) 在第 21 行的 templates/ja_zeolite/assets/functions.php 中

第 2 行是函数 parsedate($data),第 21 行是 }(函数结束)。 功能是:

function parseDate($date){
$items = explode('.', $date);
switch($items[1]){
    case 1: $mese = 'Gen'; break;
    case 2: $mese = 'Feb'; break;
    case 3: $mese = 'Mar'; break;
    case 4: $mese = 'Apr'; break;
    case 5: $mese = 'Mag'; break;
    case 6: $mese = 'Giu'; break;
    case 7: $mese = 'Lug'; break;
    case 8: $mese = 'Ago'; break;
    case 9: $mese = 'Set'; break;
    case 10: $mese = 'Ott'; break;
    case 11: $mese = 'Nov'; break;
    case 12: $mese = 'Dic'; break;
    default: $mese = '---';
}
$data_corretta = array(0 => $mese, 1 => $items[2]);
return $data_corretta;
}

我也试过改名字功能,还是不行。

为什么?

【问题讨论】:

  • 确保您不多次包含该文件(考虑使用 include_once 或 require_once)并确保没有其他文件定义具有相似名称的函数。
  • mm .. 我使用了 include_once 并且它有效,但我没有找到其他时间在哪里包含它..

标签: php function fatal-error redeclare


【解决方案1】:

您(或 Joomla)可能多次包含此文件。将您的函数包含在条件块中:

if (!function_exists('parseDate')) {
    // ... proceed to declare your function
}

【讨论】:

  • 正确答案。我讨厌不以这种方式处理它的主题。我真的很讨厌他们。
  • 为 Joomla 解决了这个问题。谢谢。
【解决方案2】:

删除函数并检查输出:

var_dump(function_exists('parseDate'));

在这种情况下,更改函数的名称。

如果你得到错误,你将包含该函数的文件两次,替换:

include

通过

include_once

并替换:

require

通过

require_once

编辑:我有点太晚了,在击败我之前发布!

【讨论】:

    猜你喜欢
    • 2011-09-12
    • 2013-05-02
    • 2011-07-17
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 2013-08-07
    相关资源
    最近更新 更多