简体   繁体   English

php中的函数内部函数:错误:无法重新声明

[英]function inside function in php: error: Cannot redeclare

I have this php script: 我有这个PHP脚本:

function hoeveelzijner ($jaar, $id)
{
            function hoeveelhoeveel($beginstamp, $endstamp, $id)
            {
                $dates = mysql_query('SELECT v_date FROM visitors WHERE id="'.$id.'" AND v_date<"'.$endstamp.'" AND v_date>"'.$beginstamp.'"');
                return mysql_num_rows($dates);
            }
$i = 1;
while ($i < 13)
{
    $hoeveel[$i-1] = hoeveelhoeveel(mktime(0, 0, 0, $i, 1, $jaar),mktime(0, 0, 0, $i, cal_days_in_month(CAL_GREGORIAN,$i,$jaar),$jaar),$id);
    $i = $i+1;
}
return $hoeveel;
}

When I put this beneath it, it works just fine: 当我把它放在它下面时,它工作得很好:

$values = hoeveelzijner(2005, 1);

However, when I do it twice, for example: 但是,当我这样做两次时,例如:

$values = hoeveelzijner(2005, 1);
$test = hoeveelzijner(2000, 4);

I get this error: Fatal error: Cannot redeclare hoeveelhoeveel() (previously declared in ...:69) in ... on line 69. 我收到此错误:致命错误:无法重新声明hoeveelhoeveel()(之前在......中声明:69)在第69行......

Anyone knows what I am doing wrong? 谁知道我做错了什么? It kinda destroys the purpose of using functions if I can only use it once... 如果我只能使用一次,它有点破坏了使用函数的目的......

Extra info: I do not include any other files, nor do I redeclare the function somewhere else in the script. 额外信息:我不包含任何其他文件,也不会在脚本中的其他位置重新声明该功能。

Thanks a lot! 非常感谢!

You can't only use it once; 你不仅可以使用一次; you can only declare it once. 你只能申报一次。 Every time the function hoeveelzijner is used, the function hoeveelhoeveel is declared. 每个功能时hoeveelzijner使用,功能hoeveelhoeveel声明。 If you call it more than once, you'll redeclare it, which is forbidden. 如果您多次调用它,您将重新声明它,这是禁止的。

You have two options: the first is to take the function definition outside the containing function. 您有两个选择:第一个是在包含函数之外的函数定义。 The function will be declared when the file is first parsed, then used repeatedly. 该函数将在首次解析文件时声明,然后重复使用。 If you want to restrict the function definition to a particular scope (a good idea in principle), you can use the anonymous function syntax introduced in PHP 5.3: 如果要将函数定义限制在特定范围内(原则上是个好主意),可以使用PHP 5.3中引入的匿名函数语法:

function hoeveelzigner($jaar, $id)
{
    $hoeveelhoeveel = function($beginstamp, $endstamp, $id)
    {
            $dates = mysql_query('SELECT v_date FROM visitors WHERE id="'.$id.'" AND v_date<"'.$endstamp.'" AND v_date>"'.$beginstamp.'"');
            return mysql_num_rows($dates);
    };

    // etc
}

You can then use the function as $hoeveelhoeveel(...) . 然后,您可以将该函数用作$hoeveelhoeveel(...)

Although PHP let's you declare a function anywhere I think what you are doing is not considered best practice. 虽然PHP允许你在任何地方声明一个函数,但我认为你所做的并不是最佳实践。

For most people it just look wrong. 对于大多数人来说,它看起来不错。

You should just declare the function outside the parent function. 您应该在父函数之外声明该函数。

Alternatively you could add a function_exists() around the inner function, although again I would just declare the inner function outside the parent function. 或者你可以在内部函数周围添加一个function_exists() ,尽管我只是在父函数之外声明内部函数。

Are perhaps even make a class for it. 或许甚至可以为它上课。

You can't declare it again, especially if the function calls it more than once. 你不能再声明它,特别是如果函数多次调用它。 Here's a simple change that should work. 这是一个应该有效的简单改变。

function hoeveelhoeveel($beginstamp, $endstamp, $id)

    $dates = mysql_query('SELECT v_date FROM visitors WHERE id="'.$id.'" AND v_date<"'.$endstamp.'" AND v_date>"'.$beginstamp.'"');
    return mysql_num_rows($dates);
}

function hoeveelzijner ($jaar, $id)
{
    $i = 1;
    while ($i < 13)
    {
        $hoeveel[$i-1] = hoeveelhoeveel(mktime(0, 0, 0, $i, 1, $jaar),mktime(0, 0, 0, $i, cal_days_in_month(CAL_GREGORIAN,$i,$jaar),$jaar),$id);
        $i = $i+1;
    }
    return $hoeveel;
}

Then you can call either function in a similar way in other functions too, if needed be. 然后,如果需要,您也可以在其他函数中以类似的方式调用任一函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM