本文介绍了无法重新声明函数php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为parseDate的函数,但是当我在我的php页面(它是一个joomla组件页面)调用它时,我得到致命错误:无法重新声明parsedate()(以前在templates / ja_zeolite / assets / functions.php中声明: 2)在第21行的templates / ja_zeolite / assets / functions.php中第2行是函数parsedate($ data),第21行是}(函数结束)。
这个函数是:

$ p $ function parseDate($ date){
$ items = explode('。 ',$ date);
switch($ items [1]){
case 1:$ mese ='Gen';打破;
案例2:$ mese ='Feb';打破;
案例3:$ mese ='Mar';打破;
案例4:$ mese ='Apr';打破;
案例5:$ mese ='Mag';打破;
案例6:$ mese ='Giu';打破;
案例7:$ mese ='Lug';打破;
案例8:$ mese ='前往';打破;
案例9:$ mese ='Set';打破;
案例10:$ mese ='Ott';打破;
案例11:$ mese ='Nov';打破;
案例12:$ mese ='Dic';打破;
默认值:$ mese ='---';
}
$ data_corretta = array(0 => $ mese,1 => $ items [2]);
返回$ data_corretta;
}

我也尝试更改名称函数,但它仍然无效。

为什么?

解决方案

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

  if(!function_exists('parseDate')){
//。 ..继续声明你的函数
}


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?

解决方案

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
}

这篇关于无法重新声明函数php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 00:54