无法重新声明函数错误 [英] Cannot redeclare a function error

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

问题描述

我正在尝试从数组的魔术引号中删除斜杠.所以我有两个功能,一个是删除斜杠,另一个是设置变量.

I am trying to remove the slashes from magic quotes from an array. So I have two functions, one is to remove the slashes, another is to set the variable.

// Strip slashes from an array.
function strip_magic_quotes($array)
{
    if (get_magic_quotes_gpc())
    {
        function stripslashes_array($array)
        {
            return is_array($array) ? array_map('stripslashes_array', $array) : stripslashes($array);
        }
        return stripslashes_array($array);
    }
    return $array;
}

function set_variable($array = array(),$key,$params = array())
{
    # If $params is not an array, let's make it array with one value of former $params.
    if(!is_array($params)) $params = array($params);
    if(!is_array($array)) parse_str($array, $array);

    # Strip slashes from the array if get_magic_quotes_gpc is on.
    $array = strip_magic_quotes($array);

    # This will return true or false.
    if(in_array('boolean', $params)) return isset($array[$key]) ? true : false;

    # This will regard '0' as a string.
    # Return value or 0 as a string.
    elseif(in_array('0', $params)) return isset($array[$key]) && ($array[$key] == '0') ? trim($array[$key]) : null;

    # Return null as string if 'null_to_string' is set.
    elseif(in_array('null_to_string', $params)) return isset($array[$key]) && !empty($array[$key]) ? trim($array[$key]) : 'null';

    # Check if the key is an array.
    elseif(isset($array[$key]) && !empty($array[$key]) && is_array($array[$key])) return isset($array[$key]) && !empty($array[$key]) ? $array[$key] : null;

    # This will regard '0', empty space as falsey.
    # Return value or null.
    else return isset($array[$key]) && !empty($array[$key]) ? trim($array[$key]) : null;
}

$array = array(
    'name'=>'Hello',
    'type'=>'{"page":"page"}'
);


# set the required array.
$items_variable = array(
    'name',
    'type'
);

# loop the array.
foreach( $items_variable as $item_variable )
{
    # set the main variables.
    $$item_variable = set_variable($array,$item_variable);
}

print_r($type);

我在实时服务器中收到了我不理解的错误消息,

I get this error in my live server which I don't understand it,

致命错误:无法重新声明stripslashes_array()(以前 在json.php:16中声明) 第16行的json.php

Fatal error: Cannot redeclare stripslashes_array() (previously declared in json.php:16) in json.php on line 16

第16行是指此行function stripslashes_array($array),似乎没有任何错误.

line 16 refers to this line function stripslashes_array($array) which does not seem to have any mistake.

有什么办法解决这个问题吗?

Any idea how to fix this?

推荐答案

发生的事情是,每次您调用函数strip_magic_quotes时,PHP都会尝试声明函数stripslashes_array.

What happens is that everytime you call the function strip_magic_quotes PHP tries to declare the function stripslashes_array.

为什么您认为要嵌套该功能?如果您确实想这样做(不应该这样做),可以将其更改为:

Why do you think you want to nest that function? If you really want to do it this way (which you shouldn't) you can change it to:

if (get_magic_quotes_gpc() && !function_exists('stripslashes_array')) {

尽管这可以解决问题,但您根本不应该首先这样做,因为:

Although this fixes the issue you simply shouldn't do it in the first place, because:

  • 为了人类的利益,最好禁用(如果可能)魔术*.
  • 几乎在所有情况下都应避免在函数中声明函数(尽管可能).

或者,如果您不需要其他地方的内部函数,只需将其完全删除即可,只需让代码在外部函数中运行即可.

Or you could simply just drop that inner function completely if you don't need it somewhere else and just let the code run in the outer function.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆