Cannot redeclare function php [duplicate]

冷暖自知 提交于 2019-11-26 09:44:16

问题


This question already has an answer here:

  • “Fatal error: Cannot redeclare <function>” 15 answers

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

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

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?


回答1:


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

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



回答2:


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 :

include

by

include_once

And replace :

require

by

require_once

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



来源:https://stackoverflow.com/questions/10930646/cannot-redeclare-function-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!