39

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?

pindol
  • 2,110
  • 6
  • 35
  • 52
  • 4
    Make sure you dont include the file more than once (consider using include_once or require_once) also make sure no other files define function with similar name. – Dmitry Kudryavtsev Jun 07 '12 at 11:14
  • 2
    mm.. i used include_once and it works, but I don't find where i include it other times.. – pindol Jun 07 '12 at 11:18

2 Answers2

101

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
}
lanzz
  • 42,060
  • 10
  • 89
  • 98
17

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 !

Legionar
  • 7,472
  • 2
  • 41
  • 70
Jean-Marie Comets
  • 706
  • 2
  • 7
  • 21