如何在php中创建一个封闭的函数?

时间:2013-10-12 12:28:06

标签: php

我想在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.

我不知道如何仅在某个函数内部创建一个变量global,而且我也不知道如何让bigfunction运行多次,因为如果我尝试像示例中那样重复运行它,那么说我不能重新声明里面的子功能。

非常感谢任何有关这些问题的帮助。

P.S。抱歉有点杂乱的格式,这是我第一次在这个页面上写,这里的格式有点混乱。

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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;
});

这是一个简单的例子。您可以使用objctes以更复杂的方式执行相同的操作。

答案 2 :(得分:0)

您是否曾考虑过使用课程来表示您的需求?闭包可能就是你想要的,然而,我读到你想要有“子功能”。这是一个可以用OOP解决的问题。如果此功能可以在代码中的其他位置重用,或者需要与系统的其他部分进行交互,请考虑将其创建为通常的类。