Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot redeclare function php [duplicate]

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?

like image 608
pindol Avatar asked Jun 07 '12 11:06

pindol


2 Answers

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
}
like image 149
lanzz Avatar answered Nov 10 '22 12:11

lanzz


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 !

like image 29
Jean-Marie Comets Avatar answered Nov 10 '22 14:11

Jean-Marie Comets