1

我想用php写一个程序,我需要做一个封闭的函数,可以从主体调用,但它有自己的变量,子函数,并且完全独立于它的环境。

基本上我需要这样的东西才能工作:

$variable=10;  //variables defined here need to be available everywhere, except in bigfunction

function smallfunction(){
    echo $variable;  //will print variable defined in main body
}

function bigfunction($input) {   
    /* this will be the big function, that needs to have its own global variables and functions. It cant see any variables defined outside of this function. Any function and variable defined in this function will never be used outside of this function, and any function or variable defined outside this function will never be used inside this function. The only interaction with its surroundings will be in its arguments and output(return) of this bigfunction. It will receive certain input, and return outcome. It will need to be run repeatedly, as it will be called rather often from main body, every time it will run independently of previous runs of this function.
    */

    $variable = $input * 5;  
    /* this variable needs to be independent of the $variable in main body, even if it has same name. It also needs to be accessible in any other function declared inside of bigfunction. Basically it has to be global variable inside bigfunction.
    */ 

    function subfunction(){
        echo $variable; //will print $variable from bigfunction
        $variable = $variable - 25; //this will change $variable value.
    }
    subfunction();
    return $variable; //bigfunction will return value equal to $input*5-25.                       
} //here the program can forget everything that happened inside bigfunction, including function declarations and defined variables.

//now these function will be called

smallfunction();  //will print value of $variable in main body. In this case it will be "10".

bigfunction(5); //will print "25" (5*5), and return 0 (25-25).

smallfunction(); //still prints 10, since $variable in main body hasn't changed.

$variable = bigfunction(8); //will print "40" (8*5), and return 15 (40-25).

smallfunction(); //this time it will print "15", since $variable value has changed with previous command.

我不知道如何仅在某个函数内使变量成为全局变量,而且我也不知道如何使 bigfunction 多次运行,因为如果我尝试像示例中那样重复运行它,它说我可以'不要重新声明它里面的子函数。

对于这些问题的任何帮助将不胜感激。

PS抱歉格式有点乱,这是我第一次在这个页面上写,这里的格式有点乱。

4

3 回答 3

1

我相信您可能希望global $variable;像这样使用关键字:

$variable=10; //variables defined here need to be available everywhere, except in bigfunction

function smallfunction(){
     global $variable;
     echo $variable;  //will print variable defined in main body
}
于 2013-10-12T12:30:47.443 回答
0

看看 PHP 的匿名函数。 http://www.php.net/manual/en/functions.anonymous.php

function bigFunction($input, Closure $callback) {
    $variable = $input * 5;
    $callback($variable);

    return $variable;
}

// will output 25;
echo bigFunction(10, function($input) {
    $input -= 25;
    return $input;
});

这是一个简单的例子。你可以用更复杂的方式来处理对象。

于 2013-10-12T12:43:27.623 回答
0

你有没有考虑过只使用一个类来表示你需要的东西?闭包可能是您正在寻找的东西,但是,我读到您想要拥有“子功能”。这是一个可以用 OOP 解决的问题。如果此功能可以在代码的其他地方重用,或者需要与系统的其他部分交互,请考虑将其创建为普通类。

于 2013-10-12T12:46:27.603 回答