php 无法重新声明函数php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10930646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:24:05  来源:igfitidea点击:

Cannot redeclare function php

phpfunctionfatal-errorredeclare

提问by pindol

I have a function called parseDate, but when i call it on my php page (it's a joomla component page) I get Fatal error: Cannot redeclare parsedate() (previously declared in templates/ja_zeolite/assets/functions.php:2) in templates/ja_zeolite/assets/functions.php on line 21

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

line 2 is function parsedate($data) and line 21 is } (end of function). The function is:

第 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;
}

I also tried to change name function, but it still doesn't work.

我也尝试更改名称功能,但它仍然不起作用。

Why?

为什么?

回答by lanzz

You (or Joomla) is likely including this file multiple times. Enclose your function in a conditional block:

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

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

回答by Jean-Marie Comets

Remove the function and check the output of:

删除该函数并检查以下输出:

var_dump(function_exists('parseDate'));

In which case, change the name of the function.

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

If you get false, you're including the file with that function twice, replace :

如果得到 false,则将包含该函数的文件两次,替换为:

include

by

经过

include_once

And replace :

并替换:

require

by

经过

require_once

EDIT : I'm just a little too late, post before beat me to it !

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